Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Your code should return the index of the first number and the index of the last number. (If their are duplicate answer, return anyone. The answer can be rorate array or non- rorate array)
Give [3, 1, -100, -3, 4]
, return [4,1]
.
分析:
此题是Continuous Subarray Sum的升级版本。对于给定序列A, Continuous Subarray Sum中,子序列为A{i -> j}, 要求 0 <= i <= j < size(A),此为经典动态规划问题。在这里,子序列除了有之前的形式外,还允许rotate subarray,即子序列允许从尾部延续到头部,形式为A{0 -> i, j -> size(A) - 1}。
解法一:
因为允许尾部到头部形成子序列。通常的想法是把数据copy一份连到尾部。此时我们可以用Continous Subarray Sum的解法来做。但是有一点区别,我们的子序列长度不能超过size(A)。因此,我们依然可以用动态规划来做,不过需要考虑到序列的长度。为了实现简便,下面给出一个O(N^2) 非动态规划的解法,利用了累计数列和条件剪枝。不过最后三个数据还是会超时。
vector<int> continuousSubarraySumII(vector<int>& A) {
// Write your code here
vector<int> result(, );
int n = A.size();
if(n < ) return result;
//duplicate array
vector<int> B(A);
B.insert(B.end(), A.begin(), A.end());
//cumsum can help to calculate the sum from B(i) to B(j) with O(1) time
vector<int> cumsum;
cumsum.push_back(B[]);
for(int i = ;i < B.size();++i)
cumsum.push_back(cumsum[i - ] + B[i]); int maxVal = B[], left = , right = ;
for(int s = ;s < n;++s){
//there is no need to start from an negative number, this pruning is useful
if(B[s] <= ) continue;
for(int e = s; e < s + n;++e){
int cur = ;
if(s == ) cur = cumsum[e];
else cur = cumsum[e] - cumsum[s - ]; if(cur > maxVal){
maxVal = cur;
left = s;
right = e;
}
}
}
result[] = left%n;
result[] = right%n;
return result;
}
解法二:
进一步分析发现,第二种subarray其实和第一种是相关的。我们可以通过剪掉最小连续子序列得到第二种subarray。这里需要注意当所有数字为负的情况。
vector<int> continuousSubarraySumII(vector<int>& A) {
// Write your code here
vector<int> result(, );
int n = A.size();
if(n < ) return result; vector<int> posMax(n, ), posMaxIdx(n, ), posMin(n, ), posMinIdx(n, );
posMax[] = A[];
posMin[] = A[];
posMaxIdx[] = ;
posMinIdx[] = ;
int sum = A[], maxVal = A[], minVal = A[],
maxL = , maxR = , minL = , minR = ; for(int i = ;i < n;++i){
sum += A[i];
//max subArray
if(posMax[i - ] > ){
posMax[i] = posMax[i - ] + A[i];
posMaxIdx[i] = posMaxIdx[i - ];
}else{
posMax[i] = A[i];
posMaxIdx[i] = i;
}
//min subArray
if(posMin[i - ] < ){
posMin[i] = posMin[i - ] + A[i];
posMinIdx[i] = posMinIdx[i - ];
}else{
posMin[i] = A[i];
posMinIdx[i] = i;
} if(posMax[i] > maxVal){
maxVal = posMax[i];
maxL = posMaxIdx[i];
maxR = i;
}
if(posMin[i] < minVal){
minVal = posMin[i];
minL = posMinIdx[i];
minR = i;
}
} int val = sum - minVal;
if(val <= maxVal || (minL == && minR == n - )){
result[] = maxL;
result[] = maxR;
}else{
result[] = minR + ;
result[] = minL - ;
} return result;
}