划分和征服最大连续子阵列(MCS)的问题

时间:2011-08-09 13:37:05

标签: java algorithm divide-and-conquer sub-array

我想为给定的整数输入数组找到一个非空的,连续的子数组,它可以有重复的值。我尝试了分而治之的方法来找到数组的最大连续子数组,它会按预期返回不同的结果。请找到以下代码。

private static int maxSumRec(int[] a, int low, int high) {
    int leftSum = 0, rightSum = 0;
    int sum = 0; 

    if (low == high) { // Base case
        return a[low]; 
    }

    int mid = (low + high) >> 1; // (low + high) / 2
    int maxLeftSum = maxSumRec(a, low, mid);
    int maxRightSum = maxSumRec(a, mid + 1, high);

    //max-crossing-subarray
    for (int i = mid; i >= low; i--) {
        sum += a[i];
        if (sum > leftSum) {
            leftSum = sum;
        }
    }
    sum = 0;
    for (int i = mid + 1; i <= high; i++) {
        sum += a[i];
        if (sum > rightSum) {
            rightSum = sum;
        }
    }
    return max3(maxLeftSum, maxRightSum, (leftSum + rightSum));
}

private static int max3(int a, int b, int c) {
    return a > b ? (a > c ? a : c) : (b > c ? b : c);
}

public static void main(String[] args) {


    //INPUT
    int a[] = {
        -5, 71, 23, 41, 34, 1, 3, 6, 2, 91, 312, 42, 31, 67, 12, 10, 18, 56, 90, 21, 45, 47, 89, 1999999990,
        78, -7, 76, 75, 74, 73, 72, 80, 24, 25, 61, 69, 84, 0, -1, 145, 1902, 200, 199, 198, 197, 196, 195, 194,
        78, 77, 76, 75, 74, 73, 72, 80, 24, 25, 61, 69, 84, 0, -1, 145, 1902, 200, 199, 198, 197, 196, 195, 194,
        5, 71, 23, 41, 34, 1, 3, 6, 2, 91, 312, 42, 31, 67, 12, 10, 18, 56, 90, 21, 45, 47, 89, 1999999990
    };

    int maxSum = maxSumRec(a, 0, a.length - 1);
    System.out.println("Max sum is " + maxSum);
}

此代码将结果返回为 2000005400 。 MCS的非递归版本返回不同的结果,即 2000010721 ,它来自{1-94}。 我无法弄清楚原因。如果代码中有错误,请告诉我。

2 个答案:

答案 0 :(得分:3)

1到95之间的总和(它是:4000010711)实际上大于1到94之间的总和。

您的ints太长了。

您需要使用long来获得正确的结果。

注意:

int介于-2147483648和2147483647之间

int test=2147483647;
System.out.println(test);
System.out.println(test+1);

你会得到:

2147483647
-2147483648

试试这个:

public class Sample5 {
        private static long maxSumRec(int[] a, int low, int high) {
            long  leftSum = 0, rightSum = 0;
            long  sum = 0; 

            if (low == high) { // Base case
                return a[low]; 
            }

            int mid = (low + high)/2; // (low + high) / 2
            long maxLeftSum = maxSumRec(a, low, mid);
            long maxRightSum = maxSumRec(a, mid + 1, high);

            //max-crossing-subarray
            for (int i = mid; i >= low; i--) {
                sum += a[i];
                if (sum > leftSum) {
                    leftSum = sum;
                }
            }
            sum = 0;
            for (int i = mid + 1; i <= high; i++) {
                sum += a[i];
                if (sum > rightSum) {
                    rightSum = sum;
                }
            }
            System.out.println("final left sum "+leftSum);
            System.out.println("final right sum "+rightSum);
            System.out.println("leftSum+rightSUM:"+(leftSum + rightSum));
            return max3(maxLeftSum, maxRightSum, (leftSum + rightSum));
        }

        private static long max3(long a, long b, long c) {
            return a > b ? (a > c ? a : c) : (b > c ? b : c);
        }

        private static int sum(int[] a,int i,int j){
            int r=0;
            for(int k=i;k<=j;k++){
                r+=a[k];
            }
            return r;
        }
        public static void main(String[] args) {


            //INPUT
            int a[] = {
                -5, 71, 23, 41, 34, 1, 3, 6, 2, 91, 312, 42, 31, 67, 12, 10, 18, 56, 90, 21, 45, 47, 89, 1999999990,
                78, -7, 76, 75, 74, 73, 72, 80, 24, 25, 61, 69, 84, 0, -1, 145, 1902, 200, 199, 198, 197, 196, 195, 194,
                78, 77, 76, 75, 74, 73, 72, 80, 24, 25, 61, 69, 84, 0, -1, 145, 1902, 200, 199, 198, 197, 196, 195, 194,
                5, 71, 23, 41, 34, 1, 3, 6, 2, 91, 312, 42, 31, 67, 12, 10, 18, 56, 90, 21, 45, 47, 89, 1999999990
            };

            long maxSum = maxSumRec(a, 0, a.length-1);
            System.out.println("Max sum is " + maxSum);

            //WITH INTS
            System.out.println("with ints, the sum 1 to 94 is " + sum(a,1,94));
            System.out.println("with ints, the sum 1 to 95 is " + sum(a,1,95));
        }


}

您将获得:

final left sum 2000005311
final right sum 2000005400
leftSum+rightSUM:4000010711
Max sum is 4000010711
with ints, the sum 1 to 94 is 2000010721
with ints, the sum 1 to 95 is -294956585

答案 1 :(得分:0)

没有足够的积分来添加评论,因此创建了快速评论的答案:

Ricky的答案大部分时间都有效,但对于像[-2,-1]这样的数组,它不起作用。只需添加:

leftSum = a [mid]; rightSum = a [mid + 1];

在你使用它们之前的某个地方它会做到这一点。