LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays

原题链接在这里:https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/

题目:

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:

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

题解:

Get the accumlated sum for nums.

Iterate from left to right to get the starting index of biggest subarray left to current index.

Iterate from right to left to get the starting index of biggest subarray right to current index.

Then for the middle part, index could be [k, n-2*k]. Iterate each of them, get the left biggest starting index and right biggest starting index. 

Keep updating the global maximum and res.

Time Complexity: O(n). n = nums.length.

Space: O(n).

AC Java:

 1 class Solution {
 2     public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
 3         int [] res = new int[3];
 4         Arrays.fill(res, -1);
 5         if(nums == null || nums.length < 3 * k){
 6             return res;
 7         }
 8         
 9         int n = nums.length;
10         int [] sum = new int[n+1];
11         for(int i = 0; i<n; i++){
12             sum[i+1] = sum[i] + nums[i];
13         }
14         
15         int [] leftPo = new int[n];
16         for(int i = k, max = sum[k] - sum[0]; i<n; i++){
17             if(sum[i+1] - sum[i+1-k] > max){
18                 max = sum[i+1] - sum[i+1-k];
19                 leftPo[i] = i+1-k;
20             }else{
21                 leftPo[i] = leftPo[i-1];
22             }
23         }
24         
25         int [] rightPo = new int[n];
26         rightPo[n-k] = n-k;
27         for(int i = n-k-1, max = sum[n] - sum[n-k]; i>=0; i--){
28             if(sum[i+k] - sum[i] >= max){
29                 max = sum[i+k] - sum[i];
30                 rightPo[i] = i;
31             }else{
32                 rightPo[i] = rightPo[i+1];
33             }
34         }
35         
36         for(int i = k, max = 0; i<=n-2*k; i++){
37             int l = leftPo[i - 1];
38             int r = rightPo[i + k];
39             if(sum[i+k] - sum[i] + sum[l+k] - sum[l] + sum[r+k] - sum[r] > max){
40                 max = sum[i+k] - sum[i] + sum[l+k] - sum[l] + sum[r+k] - sum[r];
41                 res[0] = l;
42                 res[1] = i;
43                 res[2] = r;
44             }
45         }
46         
47         return res;
48     }
49 }

类似Best Time to Buy and Sell Stock III.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12010372.html