Chapter 87 LeetCode refers to the maximum sum of continuous subarrays of Offer dynamic programming

Keywords: Algorithm leetcode Dynamic Programming

1. Title Description

Enter an integer array. One or more consecutive integers in the array form a sub array. Find the maximum value of the sum of all subarrays.

The required time complexity is O(n).

Example 1:

Input: num = [- 2,1, - 3,4, - 1,2,1, - 5,4]
Output: 6
Explanation: the maximum sum of continuous subarray [4, - 1,2,1] is 6.

Tips:

1 <= arr.length <= 10^5
-100 <= arr[i] <= 100

Source: LeetCode
Link: https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

2. Problem solving steps of dynamic programming

2.1. Definition of DP [i]

For the above question, let's take a number, such as 8, then dp[8] represents the maximum sum of consecutive subarrays containing numbers with subscript 8.
dp[i] represents the maximum sum of consecutive subarrays containing numbers with subscript I.

2.2. Recursive

If you have watched the video of the up Master of station b (dynamic planning 1 and dynamic planning 2), you should remember a particularly interesting word, choose or not, for this question
This example:

value-21-34-12,1-54
subscript01234567

We can think like this,
If we choose the last 4 (subscript 7), the maximum sum of continuous subarrays is maxsum = the maximum sum of continuous subarrays containing numbers with subscript 7 (this should be calculated (calculated)); The calculation is as follows:
maxsum1 = sum(nums[0...7] )
maxsum2 = sum(nums[1...7] )
maxsum3 = sum(nums[2...7] )
maxsum4 = sum(nums[3...7] )
maxsum5 = sum(nums[4...7] )
maxsum6 = sum(nums[5...7] )
maxsum7 = sum(nums[6...7] )
maxsum8 = sum(nums[7...7] )
There are eight cases. Take the maximum maxsum.

If 4 is not selected, the result is the maximum sum of consecutive subarrays with - 5 (subscript 6).
Then compare the two results and take the maximum value.
Therefore, our recurrence formula is:
dp[i] = max(maxsum,dp[i-1]).

2.3.dp initialization and recursive function exit

If the array has only one element, the maximum sum is num [0], and the exit is i == 0, Num [0] is returned.

2.4. Recursive function

int dp(vector<int> nums,int i){
    if(i == 0) {
        return nums[0];
    }
    
    int maxSum = INT_MIN;
    int sum = 0;
    for(int j = i;j >= 0;j--){
        sum += nums[j];
        if(sum > maxSum) {
            maxSum = sum;
        }
    }
    int max2 = dp(nums,i-1);
    return max(maxSum,max2);
}

Although the timeout, it shows that our idea is right and not wrong.

2.5. Change recursion to iteration

int dp_iterate(vector<int> nums){
    int n = nums.size();
    vector<int> dp(n,INT_MIN);
    dp[0] = nums[0];
    for(int i = 1;i < n;i++){
        maxSum = max(nums[i],maxSum + nums[i]);
        int maxSum = INT_MIN;
	    int sum = 0;
	    for(int j = i;j >= 0;j--){
	        sum += nums[j];
	        if(sum > maxSum) {
	            maxSum = sum;
	        }
	    }
        dp[i] = max(dp[i-1],maxSum);
    }
    
    return dp[n-1];
}

It's too slow because the complexity is O (n^2).

2.6. Iterative optimization

The results are sequential. We just need to keep the current results and compare them with the next one.

int dp_iterate_better(vector<int> nums){
    int n = nums.size();
    int ans = nums[0];
    for(int i = 1;i < n;i++){
        maxSum = max(nums[i],maxSum + nums[i]);
        int maxSum = INT_MIN;
	    int sum = 0;
	    for(int j = i;j >= 0;j--){
	        sum += nums[j];
	        if(sum > maxSum) {
	            maxSum = sum;
	        }
	    }
        ans = max(ans,maxSum);
    }
    
    return dp[n-1];
}

Space is optimized, but time is not optimized.

Here is the time optimization:
We use maxSum to record the maximum sum of continuous subarrays ending in i - 1. When we calculate the maximum sum of continuous subarrays ending in I, we only need to add num [i] to get the maximum sum of continuous subarrays ending in I, and then compare it with num [i] (this is also the maximum sum of continuous subarrays ending in I), That is, the maximum sum of continuous sub arrays ending with I can be obtained, and then compared with the result. The time complexity is O(n)

int dp_opt_better2(vector<int> nums) {
    int n = nums.size();
    int ans = nums[0];
    int maxSum = nums[0];
    for(int i = 1;i < n;i++){
        maxSum = max(maxSum + nums[i],nums[i]);
        ans = max(ans,maxSum);
    }
    
    return ans;
}

3. Conclusion

Write space optimization should be good.

Posted by Shaudh on Sun, 05 Dec 2021 04:53:43 -0800