美文网首页
689. Maximum Sum of 3 Non-Overla

689. Maximum Sum of 3 Non-Overla

作者: 冷殇弦 | 来源:发表于2017-10-03 02:25 被阅读0次

In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum.
Each subarray will be of size k, and we want to maximize the sum of all 3*k entries.
Return the result as a list of indices representing the starting position of each interval (0-indexed). If there are multiple answers, return the lexicographically smallest one.
Example:

Input: [1,2,1,2,6,7,5,1], 2
Output: [0, 3, 5]
Explanation: Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5].
We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger.

Note:

  1. nums.length will be between 1 and 20000.
  2. nums[i] will be between 1 and 65535.
  3. k will be between 1 and floor(nums.length / 3).

这题有点难,还没完全理解。分别求左,右区间的DP值。Index有点绕。最后求三个区间和的最大值。
以下答案解答比较好:
The question asks for three non-overlapping intervals with maximum sum of all 3 intervals. If the middle interval is [i, i+k-1], where k <= i <= n-2k, the left interval has to be in subrange [0, i-1], and the right interval is from subrange [i+k, n-1].
So the following solution is based on DP.

posLeft[i] is the starting index for the left interval in range [0, i];
posRight[i] is the starting index for the right interval in range [i, n-1];

Then we test every possible starting index of middle interval, i.e. k <= i <= n-2k, and we can get the corresponding left and right max sum intervals easily from DP. And the run time is O(n).

class Solution {
    public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
        int n = nums.length;
        int[] sum = new int[n+1];
        for(int i=0;i<n;i++) sum[i+1] = sum[i] + nums[i];
        int[] posLeft = new int[n], posRight = new int[n];
        // left start from 0 to n-k to get max sum left interval values
        for(int i=k, total=sum[k]-sum[0]; i<n;i++){
            if(sum[i+1]-sum[i-k+1]>total){
                total = sum[i+1]-sum[i-k+1];
                posLeft[i] = i-k+1;
            }else{
                posLeft[i] = posLeft[i-1];
            }
        }
        
        // right start from 0 to n-k to get max sum right interval values
        posRight[n-k]=n-k;
        for(int i=n-k-1, total=sum[n]-sum[n-k]; i>=0;i--){
            if(sum[k+i]-sum[i]>total){
                total = sum[k+i]-sum[i];
                posRight[i] = i;
            }else{
                posRight[i] = posRight[i+1];
            }
        }
        
        // test all middle interval
        int maxsum = 0;
        int[] ans = new int[3];
        for(int i=k;i<=n-2*k;i++){
            int l = posLeft[i-1], r = posRight[i+k];
            int total = (sum[i+k]-sum[i]) + (sum[l+k]-sum[l]) + (sum[r+k]-sum[r]);
            if(total>maxsum){
                maxsum = total;
                ans[0]=l; ans[1]=i; ans[2]=r;
            }
        }
        return ans;
    }
}

相关文章

网友评论

      本文标题:689. Maximum Sum of 3 Non-Overla

      本文链接:https://www.haomeiwen.com/subject/ejjxyxtx.html