与分而治之最大连续子阵问题(MCS) [英] Problem with Divide and Conquer Maximum Consecutive Subarray (MCS)

查看:224
本文介绍了与分而治之最大连续子阵问题(MCS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to find a nonempty, contiguous subarray for a given input array of integers, that can have duplicate values. I tried the divide and conquer method to find the maximum consecutive subarray of an array, which returns a different result as expected. Please find the code below.

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);
}

这code返回结果为 2000005400 。 MCS的非递归版本返回不同的结果,即 2000010721 和它发现从{1-94}。 我无法找出原因。请让我知道,如果有在code的错误。

This code returns the result as 2000005400. The non recursive version of MCS returns a different result i.e. 2000010721 and its found from {1-94}. I'm unable to figure out the reason. Please let me know if there's a bug in the code.

推荐答案

从1到95的总和(它是:4000010711)。其实比一个从1到94更大

The sum from 1 to 95 (it's :4000010711 ) is actually greater than the one from 1 to 94.

整数过长。

您需要使用,以获得正确的结果。

You need to use long to get the right result.

注意:

INT是-2147483648和2147483647之间

int are between -2147483648 and 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

这篇关于与分而治之最大连续子阵问题(MCS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆