[英]Is there enough information retained in Kadane's algorithm to return the actual maximum subarray or indices, rather than just sum?
这是 Kadane 算法的 Java 实现,用于找到具有最大总和的连续子数组的总和。
static int maxSum(int[] arr) {
int maxEndingHere = arr[0];
int maxGlobal = arr[0];
for (int i = 1; i < arr.length; i++) {
maxEndingHere = Math.max(arr[i], maxEndingHere + arr[i]);
maxGlobal = Math.max(maxGlobal, maxEndingHere);
}
return maxGlobal;
}
这只是返回总和。 我想要实际的子阵列。 不过,这些信息似乎丢失了。 我尝试在本地最大值重置时更新开始索引,并在全局最大值更新时更新结束索引,但在这种情况下失败:
int[] arr = {-57, -10000, -1, -4, -45, -6, -9, -19, -16, -17};
注意这里有一个类似的问题: How to return maximum sub array in Kadane's algorithm?
但据我所知,在总和为负的情况下,每个答案都会失败。
因为你在这里标记了algorithm
是一个解释,然后是一个python
实现。
这个问题是 Kadane 算法的直接扩展。 Kadane的算法如下:
for each item in arr:
current_max = max(current_max + item, item)
global_max = global_max(current_max, global_max)
我们只需要记录当前和全局最大值更新时的索引:
for each item in arr:
# updating current max and keeping track current of start and end indices
current_max = max(current_max + item, item)
if item is new current_max: set current_start_index to this index
set current_end_index to this index
# keep track of global start and end indices
global_max = max(global_max, current_max)
if global_max has been updated:
set global_start to current_start
set global_end to current_end
Python实现:
def maxSum(arr):
cur_max = glob_max = float('-inf')
glob_start = glob_end = cur_start = -1
for index, item in enumerate(arr):
if item > cur_max + item:
cur_max = item
cur_start = index
else:
cur_max += item
if cur_max > glob_max:
glob_max = cur_max
glob_start = cur_start
glob_end = index
return arr[glob_start:glob_end+1]
一些测试用例:
arr = [-57, -10000, -1, -4, -45, -6, -9, -19, -16, -17]
arr = [-1, 2, -1, 20, -4, -5, -6, -9, -19, -16, -17]
输出:
[-1]
[2, -1, 20]
请注意,如果您想考虑空的连续子数组,只需在末尾添加一个检查 - 如果全局最大值小于 0,则返回一个空数组。
最后一些额外的代码来证明算法是正确的:
def kadane(arr):
a = b = float('-inf')
for i in arr:
a = max(i, a+i)
b = max(a, b)
return b
from random import random
for _ in range(10000):
arr = [random()-0.5 for _ in range(50)]
assert kadane(arr) == sum(maxSum(arr))
这将创建具有正负的随机数组,并断言输出数组的总和等于 kadane 算法的常规版本的输出。
这里也是在 C++ 中的实现,包括 Kadane(实际上只是动态编程方法)和使用索引计算和一些注释扩展的 Kadane:
int maxSubArray(vector<int>& nums)
{
int n = nums.size();
if(n == 0) return INT_MIN;
// max sum that ends at index I
int sumMaxI = nums[0];
// total max sum
int sumMax = nums[0];
for(int i = 1; i < n; i++)
{
int curr = nums[i];
// calc current max sum that ends at I
int currSumMaxI = sumMaxI + curr;
// calc new max sum that ends at I
sumMaxI = max(currSumMaxI, curr);
// calc new total max sum
sumMax = max(sumMax, sumMaxI);
}
return sumMax;
}
int maxSubArray_findBeginEnd(vector<int>& nums)
{
int n = nums.size();
if(n == 0) return INT_MIN;
// max sum that ends at index I
int sumMaxI = nums[0];
// start index for max sum (end index is I)
int sumMaxIStart = 0;
// total max sum
int sumMax = nums[0];
// start and end index for total max sum
int sumMaxStart = 0;
int sumMaxEnd = 0;
for(int i = 1; i < nums.size(); i++)
{
int curr = nums[i];
// calc current max sum that ends at I
int currSumMaxI = sumMaxI + curr;
// calc new min sum that ends at I and its starting index
// this part is equal to: sumMaxI = max(currSumMaxI, curr);
// but additionaly enables to save new start index as well
if(curr > currSumMaxI)
{
sumMaxI = curr;
sumMaxIStart = i;
}
else
sumMaxI = currSumMaxI;
// calculate total max sum
// this part is equal to: sumMax = max(sumMax, sumMaxI);
if(sumMaxI > sumMax)
{
sumMax = sumMaxI;
sumMaxStart = sumMaxIStart;
sumMaxEnd = i;
}
// this part is to additionaly capture longest subarray among all that have max sum
// also, of all subarrays with max sum and max len, one with smallest index
// will be captured
else if(sumMaxI == sumMax)
{
if(i - sumMaxIStart > sumMaxEnd - sumMaxStart)
{
sumMaxStart = sumMaxIStart;
sumMaxEnd = i;
}
}
}
// check validity of start and end indices
int checkSum = 0;
for(int i = sumMaxStart; i <= sumMaxEnd; i++)
checkSum += nums[i];
assert(checkSum == sumMax);
// output indices
cout << "Max subarray indices: [" << sumMaxStart << "," << sumMaxEnd << "]" << endl;
return sumMax;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.