Kadane算法中如何返回最大子数组?
public class Kadane {
double maxSubarray(double[] a) {
double max_so_far = 0;
double max_ending_here = 0;
for(int i = 0; i < a.length; i++) {
max_ending_here = Math.max(0, max_ending_here + a[i]);
max_so_far = Math.max(max_so_far, max_ending_here);
}
return max_so_far;
}
}
上面的代码返回最大子数组的和。
我该如何返回具有最大总和的子数组?
public class Kadane {
double maxSubarray(double[] a) {
double max_so_far = 0;
double max_ending_here = 0;
for(int i = 0; i < a.length; i++) {
max_ending_here = Math.max(0, max_ending_here + a[i]);
max_so_far = Math.max(max_so_far, max_ending_here);
}
return max_so_far;
}
}
The above code returns the sum of the maximum sub-array.
How would I instead return the sub-array which has the maximum sum?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
像这样的事情:
OPDATE:还处理数组中所有负数的情况,并且总和的默认值为 0。
Something like this:
UPDATE by the OP: also handle the cases with all negative numbers in the array and default of the sum is 0.
上面的代码有一个错误。应该是:
NOT:
如果不是,则会失败,例如: 2 , 4, 22, 19, -48, -5 , 20, 40 并返回 55 而不是正确答案 60。
参见 Kadane 算法 http://en.wikipedia.org/wiki/Maximum_subarray_problem
The code above has an error. Should be:
NOT:
If not, would fail for a sequence such as: 2 , 4, 22, 19, -48, -5 , 20, 40 and return 55 instead of the correct answer of 60.
SEE Kadane algorithm at http://en.wikipedia.org/wiki/Maximum_subarray_problem
我将 max_so_far 维护在一个列表中:
然后搜索列表中最大的总和,其索引作为子序列结束。
从索引开始向后查找,找到最后一个值为正的索引。后续的开始就是这个索引。
I maintain the max_so_far in a list:
Then search the biggest sum in list, its index as sub sequnece end.
Start from index as end and search backwards, find the last index whose value is positive. Subsequence start is this index.
与算法密切相关的更简单的方法。
一旦有了开始和结束索引。
A more easier approach closely linked to the algorithm.
Once you have start and End index.
我们可以使用以下代码来跟踪最大子数组:
PS 假设给定数组是最大子数组问题的候选者,并且所有元素都不为负
we can keep track max subarray by using following code :
P.S. Assume that given array is candidate of Max sub array problem as well as not having all elements negative
每次启动新的子数组和时更新可能的左(起始)索引。 max_sum 更新后,更新最终的左和右(结尾)。还维护一个触发器,告知是否创建了新的子数组和。
Update the probable left(starting) index every time a new sub-array sum is initiated. Update the final left and right(ending) once the max_sum is updated. Also maintain a trigger that tells if a new sub-array sum is created.
我知道这是一个旧线程,但为了清楚起见,我想分享我的解决方案版本。
I know this is an old thread, but wanted to share my version of the solution for clarity.
C++ 中的另一个实现,Kadane(实际上只是动态编程方法)和带有索引计算和一些注释的扩展 Kadane:
Another implementation in C++, both Kadane (which is actually just dynamic programming approach) and extended Kadane with indices calculation and some comments:
当我们确实得到 max_so_far 时,更新 max_start_index 和 max_end_index 。 start_index 不断变化,因此,跟踪它并在找到 max 时将其存储到 max_start_index 中。
Update the max_start_index and max_end_index when we indeed got the max_so_far. start_index keeps changing so, keep track of it and store it into max_start_index when you found max.