Continuous Subarray Sum I/II

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:

  1. 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+11003+4)(100+3)=8.

Thus, in circular problem, we need to calculate two result, and compare to get the larger result.

Tips/Notes:

  1. 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;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值