分而治之最大连续子数组 (MCS) 问题
我想为给定的整数输入数组找到一个非空的连续子数组,它可以有重复的值。我尝试使用分而治之的方法来查找数组的最大连续子数组,它返回与预期不同的结果。请找到下面的代码。
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} 中找到的结果。 我无法找出原因。如果代码中有错误,请告诉我。
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);
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1 到 95 的总和(即 :4000010711 )实际上大于 1 到 94 的总和。
您的
int
太长了。您需要使用
long
才能获得正确的结果。注意:
int 介于 -2147483648 和 2147483647 之间,
您将得到:
试试这个:
您将得到:
The sum from 1 to 95 (it's :4000010711 ) is actually greater than the one from 1 to 94.
Your
ints
are too long.You need to use
long
to get the right result.Note:
int are between -2147483648 and 2147483647
you will get:
Try this :
You will get :
没有足够的点来添加评论,因此创建了一个快速评论的答案:
Ricky 的答案在大多数情况下都有效,但对于 [-2,-1] 等数组则不起作用。只需添加:
leftSum = a[mid];rightSum = a[mid+1];
在你使用它们之前的某个地方,它就会起作用。
dont have enough points to add a comment so created an answer for a quick comment:
Ricky's answer works most of the time but for array such as [-2,-1] it won't work. simply add:
leftSum = a[mid];rightSum = a[mid+1];
somewhere before you use them and it'll do the trick.