maximum subarray sum是计算机科学中的着名问题。
至少有两种解决方案:
在视频tutorial中,作者提到蛮力方法为O(n^2)
,阅读another answer一个人认为O(n^2)
而另一个人认为它是O(n^3)
< / p>
蛮力O(n^2)
还是O(n^3)
?更重要的是,你可以说明你对蛮力方法进行的分析,以了解它是O(?)
吗?
答案 0 :(得分:19)
嗯,这取决于力量的粗暴程度。
如果我们生成所有(i, j): i <= j
对并计算其间的总和,则为O(n^3)
:
....
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++) {
int sum = 0;
for (int k = i; k <= j; k++)
sum += a[k];
if (sum > max)
max = sum;
}
如果我们从所有位置开始并计算运行总和,则为O(n^2)
:
....
for(int i = 0; i < n; i++) {
int sum = 0;
for (int j = i; j < n; j++) {
sum += a[j];
if (sum > max)
max = sum;
}
}
答案 1 :(得分:3)
以下是最大子阵列和问题的三种解决方案。 solve1()
在O(N)时间运行,solve2()
在O(N ^ 2)中运行,solve3()
在O(N ^ 3)中运行。请注意,solve1()
称为Kadane的算法。
O(N ^ 2)和O(N ^ 3)函数之间的区别在于,在O(N ^ 2)函数中,每次end
索引递增时,隐式计算和,而在O(N ^ 3)函数中,总和是使用start
和end
之间的第三个显式循环计算的。
我还在所有三种方法中添加了代码来处理所有输入值为负的情况。
public class MaximumSubarraySum {
/**
* Solves the maximum subarray sum in O(N) time.
*/
public static int solve1(int[] input) {
int sum = input[0];
int bestSum = sum;
for (int i = 1; i < input.length; i++) {
sum = Math.max(input[i], input[i] + sum);
bestSum = Math.max(sum, bestSum);
}
return bestSum;
}
/**
* Solves the maximum subarray sum in O(N^2) time. The two indices
* 'start' and 'end' iterate over all possible N^2 index pairs, with
* the sum of input[start, end] always computed for every 'end' value.
*/
public static int solve2(int[] input) {
int bestSum = -Integer.MAX_VALUE;
for (int start = 0; start < input.length; start++) {
// Compute the sum of input[start, end] and update
// 'bestSum' if we found a new max subarray sum.
// Set the sum to initial input value to handle edge case
// of all the values being negative.
int sum = input[start];
bestSum = Math.max(sum, bestSum);
for (int end = start+1; end < input.length; end++) {
sum += input[end];
bestSum = Math.max(sum, bestSum);
}
}
return bestSum;
}
/**
* Solves the maximum subarray sum in O(N^3) time. The two indices
* 'start' and 'end' iterate over all possible N^2 index pairs, and
* a third loop with index 'mid' iterates between them to compute
* the sum of input[start, end].
*/
public static int solve3(int[] input) {
int bestSum = -Integer.MAX_VALUE;
for (int start = 0; start < input.length; start++) {
for (int end = start; end < input.length; end++) {
// Compute the sum of input[start, end] using a third loop
// with index 'mid'. Update 'bestSum' if we found a new
// max subarray sum.
// Set the sum to initial input value to handle edge case
// of all the values being negative.
int sum = input[start];
bestSum = Math.max(sum, bestSum);
for (int mid = start+1; mid < end; mid++) {
sum = Math.max(input[mid], input[mid] + sum);
bestSum = Math.max(sum, bestSum);
}
}
}
return bestSum;
}
public static void runTest(int[] input) {
System.out.printf("\n");
System.out.printf("Input: ");
for (int i = 0; i < input.length; i++) {
System.out.printf("%2d", input[i]);
if (i < input.length-1) {
System.out.printf(", ");
}
}
System.out.printf("\n");
int result = 0;
result = MaximumSubarraySum.solve1(input);
System.out.printf("solve1 result = %d\n", result);
result = MaximumSubarraySum.solve2(input);
System.out.printf("solve2 result = %d\n", result);
result = MaximumSubarraySum.solve3(input);
System.out.printf("solve3 result = %d\n", result);
}
public static void main(String argv[]) {
int[] test1 = { -2, -3, 4, -1, -2, -1, -5, -3 };
runTest(test1);
int[] test2 = { -2, -3, -4, -1, -2, -1, -5, 3 };
runTest(test2);
int[] test3 = { -2, -3, -4, -1, -2, -1, -5, -3 };
runTest(test3);
int[] test4 = { -2, -3, 4, -1, -2, 1, 5, -3 };
runTest(test4);
}
}
输出结果为:
Input: -2, -3, 4, -1, -2, -1, -5, -3
solve1 result = 4
solve2 result = 4
solve3 result = 4
Input: -2, -3, -4, -1, -2, -1, -5, 3
solve1 result = 3
solve2 result = 3
solve3 result = 3
Input: -2, -3, -4, -1, -2, -1, -5, -3
solve1 result = -1
solve2 result = -1
solve3 result = -1
Input: -2, -3, 4, -1, -2, 1, 5, -3
solve1 result = 7
solve2 result = 7
solve3 result = 7
答案 2 :(得分:-4)
这可以用O(N)完成,如下! 我错过了什么吗?
int[] arr = {}; //elements;
int max = 0, temp = 0;
for (int i = 0; i < arr.length; i++) {
temp = Math.max(arr[i], arr[i] + temp);
max = Math.max(temp, max);
}
System.out.println(max); //result