java中的kadane算法

发布于 2024-12-01 20:24:31 字数 729 浏览 1 评论 0原文

我在 java 中有以下 Kadane 算法的实现。基本上是找到连续子数组的最大和。

String[] numbers = string.split(",");
                int max_so_far = 0;
                int max_ending_here = 0;
                for (int i = 0; i < numbers.length-1;i++){
                     max_ending_here = max_ending_here + Integer.parseInt(numbers[i]);
                     if (max_ending_here < 0)
                         max_ending_here = 0;
                     if (max_so_far < max_ending_here)
                          max_so_far = max_ending_here;
                }
                System.out.println(max_so_far);

但是,如果数组中存在负数和正数的组合,则此方法不起作用,例如以下内容:

2,3,-2,-1,10

应返回 12 作为最大值。截至目前,它返回 5

I have the following implementation of Kadane's algorithm in java. It is basically to find the maximum sum of contiguous subarray.

String[] numbers = string.split(",");
                int max_so_far = 0;
                int max_ending_here = 0;
                for (int i = 0; i < numbers.length-1;i++){
                     max_ending_here = max_ending_here + Integer.parseInt(numbers[i]);
                     if (max_ending_here < 0)
                         max_ending_here = 0;
                     if (max_so_far < max_ending_here)
                          max_so_far = max_ending_here;
                }
                System.out.println(max_so_far);

However this doesn't work if there is a combination of a negative and positive number in an array, for example the following:

2,3,-2,-1,10

Which should return a 12 as a maximum. As of now it returns 5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

与他有关 2024-12-08 20:24:32

您的算法实现看起来不错,但是您的循环条件 i i i i i i i i i Numbers.length-1 则不然:它在距数组末尾仅 1 处停止。 <代码>我< numbers.length 应该可以做到:-)

You algorithm implementation looks ok, but your loop conditional i < numbers.length-1 does not: it stops just 1 short of the end of the array. i < numbers.length should do it :-)

一个人的旅程 2024-12-08 20:24:32

这对我有用:

    String string = "2,3,-2,-1,10";
    String[] numbers = string.split(",");
    int max_so_far = 0;
    int max_ending_here = 0;
    for (String num : numbers) {
        int x = Integer.parseInt(num);
        max_ending_here = Math.max(0, max_ending_here + x);
        max_so_far = Math.max(max_so_far, max_ending_here);
    }
    System.out.println(max_so_far);

this works for me:

    String string = "2,3,-2,-1,10";
    String[] numbers = string.split(",");
    int max_so_far = 0;
    int max_ending_here = 0;
    for (String num : numbers) {
        int x = Integer.parseInt(num);
        max_ending_here = Math.max(0, max_ending_here + x);
        max_so_far = Math.max(max_so_far, max_ending_here);
    }
    System.out.println(max_so_far);
优雅的叶子 2024-12-08 20:24:32

关于 Michał Šrajer 的上述回答:

第 7 行: max_ending_here = Math.max(0, max_ending_here + x);

应该是:

max_ending_here = Math.max(x, max_ending_here + x);

...根据此处定义的 Kadane 算法

Regarding the above answer by Michał Šrajer:

Line #7: max_ending_here = Math.max(0, max_ending_here + x);

should be:

max_ending_here = Math.max(x, max_ending_here + x);

...according to the Kadane algorithm as defined here

空名 2024-12-08 20:24:32

为时已晚,但如果将来有人需要的话。

public static void kadaneAlgo(int[][] array)
    for(int i = 1; i < array.length; i++){
            int max_value_index_i = numberOrSum(array[i], past);
            if(max_value_index_i > sum){
                sum = max_value_index_i;
            }
            past = max_value_index_i;

        }
        System.out.println("Max sum from a contiguous sub array is : " + sum);
    }

    public static int numberOrSum(int number, int past){
        return Math.max(number, number+past);
    }

Too late, but if someone needs it in the future.

public static void kadaneAlgo(int[][] array)
    for(int i = 1; i < array.length; i++){
            int max_value_index_i = numberOrSum(array[i], past);
            if(max_value_index_i > sum){
                sum = max_value_index_i;
            }
            past = max_value_index_i;

        }
        System.out.println("Max sum from a contiguous sub array is : " + sum);
    }

    public static int numberOrSum(int number, int past){
        return Math.max(number, number+past);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文