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

时间: 2024-08-30 07:21:39

LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays的相关文章

[LeetCode] 689. 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 p

【leetcode】689. 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 star

Leetcode 1031 Maximum Sum of Two Non-Overlapping Subarrays (滑动窗口)

Leetcode 1031 题目描述 Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L and M. (For clarification, the L-length subarray could occur before or after the M-le

689. Maximum Sum of 3 Non-Overlapping Subarrays

Max Sum of Subarray with size k, 相当于Easy难度,我说用一个sum array存sum,然后做减法就行.中国小哥说让优化空间,于是说可以只用两个数. 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 t

689. 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 star

Leetcode Week5 Maximum Sum Circular Subarray

Question Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty subarray of C. Here, a circular array means the end of the array connects to the beginning of the array.  (Formally, C[i] = A[i] when 0 <= i

Leetcode 1191. K-Concatenation Maximum Sum

Description: Given an integer array arr and an integer k, modify the array by repeating it k times. For example, if arr = [1, 2] and k = 3 then the modified array will be [1, 2, 1, 2, 1, 2]. Return the maximum sub-array sum in the modified array. Not

Leetcode 1191 K-Concatenation Maximum Sum 动态规划

Leetcode 1191 K-Concatenation Maximum Sum 动态规划 题目描述 Given an integer array arr and an integer k, modify the array by repeating it k times. For example, if arr = [1, 2] and k = 3 then the modified array will be [1, 2, 1, 2, 1, 2]. Return the maximum s

[LeetCode] 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 p