402. Continuous Subarray Sums
Tag:
Subarray, array
Main Idea:
Partition problem. The main idea is that if the sum from 0 … i is less than 0, then start the range from i + 1. Detail explained in the comment of code.
Tips/Notes:
- Notice the initialization of sum and res_sum.
Time/Space Cost:
Time Cost:
O
(
n
)
\ O(n)
O(n)
Space Cost:
O
(
1
)
\ O(1)
O(1)
Code:
class Solution {
public:
/*
* @param A: An integer array
* @return: A list of integers includes the index of the first number and the index of the last number
*/
vector<int> continuousSubarraySum(vector<int> &A) {
// write your code here
//initialize the res as [0,0]
vector<int> res = {0 ,0};
int res_sum = A[0];
int sum = 0, l = 0;
for(int r = 0; r < A.size(); r++){
// if the sum is less than 0, range from the next element
if(sum < 0){
l = r;
sum = A[r];
}
// if the sum is larger than 0, keep adding
else
sum += A[r];
// compare the sum with the res_sum
if(sum > res_sum){
res_sum = sum;
res[0] = l;
res[1] = r;
}
}
return res;
}
};
Follow-up Problem: 403. Continuous Subarray Sum II
Main Idea:
Classic subarray follow-up question. As we discuss in Subarray Sum, it’s common to transform the subarray into 2D matrix problem. In this one, it’s to make the array circular to find out our target result.
Based on the original problem, we have a clue how to find the maximum sum range. In circular array, one of the solution is to negate the result.
Before discussing, let sum be the sum of all elements in the array; min_sum be the minimum sum in the array, which is opposite with max_sum.
Let take this problem as example. When the array is circular, we could have two possibilities of result. One is in the non-circular one, our result is max_sum, which is the result of the previous problem; The other is in circular one, it means the answer begin from the tail to the head. Instead of calculating max_sum, we are going to get sum and min_sum, and our result would be sum - min_sum.
For example, the result of [3, 1, -100, -3, 4] is ( 3 + 1 − 100 − 3 + 4 ) − ( − 100 + 3 ) = 8 (3 + 1 -100 -3 +4 ) - (-100 + 3) = 8 (3+1−100−3+4)−(−100+3)=8.
Thus, in circular problem, we need to calculate two result, and compare to get the larger result.
Tips/Notes:
- Pay attention to negation situation, the position of l and r can be tricky. First, it cannot be the negate of [0, n-1]; Second, in case l == 0 OR r == n-1, we need to MOD the position.
Time/Space Cost:
Time Cost:
O
(
n
)
\ O(n)
O(n)
Space Cost:
O
(
1
)
\ O(1)
O(1)
Code:
class Solution {
public:
/*
* @param A: An integer array
* @return: A list of integers includes the index of the first number and the index of the last number
*/
vector<int> continuousSubarraySumII(vector<int> &A) {
// write your code here
vector<int> res = {0, 0};
int n = A.size();
if(n == 0) return res;
int sum = 0, res_sum = A[0], l = 0, total = 0;
for(int r = 0; r < n; r++){
total += A[r];
if( sum < 0 ){
sum = A[r];
l = r;
}
else {
sum += A[r];
}
if(sum > res_sum){
res_sum = sum;
res[0] = l;
res[1] = r;
}
}
// above is the same with previous problem
// except in the for-loop, I use total to calculate the array sum
// below is the negation of max_sum
// main idea is the similar, but pay attention to position update
int min_sum = 0, min_res_sum = A[0];
l = 0;
for(int r = 0; r < n; r++){
if(min_sum > 0){
l = r;
min_sum = A[r];
}
else {
min_sum += A[r];
}
if(min_sum < min_res_sum){
min_res_sum = min_sum;
// pay attention here, detailed explained in tips
if(total - min_res_sum > res_sum && !(l == 0 && r == n-1 ) ){
res_sum = total - min_res_sum;
res[0] = (r + 1) % n;
res[1] = (l - 1 + n) % n;
}
}
}
return res;
}
};