动态规划——Maximum Sum of 3 Non-Overlapping Subarrays

这个题对我来说真的是相当难的题目了,严格来讲可能不算是个动态规划的题目,但这个题目对类似的划分多个非重叠连续子区间的问题提供了一个很好解决方案

这个题目需要找三个非重叠的连续子区间,通过维护两个数组将第一个和第三个子区间可能的开始pos记录下来,在中间那个子区间开始的pos遍历时限制其边界范围,根据长度能恰到好处的将三个子区间划分成非重叠的,还使用了集合相减代替累加这样比较简单好用的技巧

下面提供代码:

 1 class Solution {
 2     public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
 3         int len = nums.length;
 4         int[]sum = new int[len+1];
 5         for(int i = 0;i<len;i++)
 6             sum[i+1] = sum[i]+nums[i];
 7         int[]posLeft = new int[len];
 8         int[]posRight = new int[len];
 9         posLeft[k-1] = 0;
10         int total = sum[k]-sum[0];
11         for(int i = k;i<len;i++) {
12             if(total<sum[i+1]-sum[i+1-k]) {
13                 total = sum[i+1]-sum[i+1-k];
14                 posLeft[i] = i-k+1;
15             }else posLeft[i] = posLeft[i-1];
16         }
17         posRight[len-k] = len-k;
18         total = sum[len]-sum[len-k];
19         for(int i = len-k-1;i>=0;i--) {
20             if(total<sum[i+k]-sum[i]) {
21                 total = sum[i+k]-sum[i];
22                 posRight[i]= i;
23             }else posRight[i] = posRight[i+1];
24         }
25         int l = 0,r = 0,max = 0;
26         int[]arr = new int[3];
27         for(int i = k;i<=len-2*k;i++) {
28             l = posLeft[i-1];
29             r = posRight[i+k];
30             total = (sum[l+k]-sum[l])+(sum[i+k]-sum[i])+(sum[r+k]-sum[r]);
31             if(total>max) {
32                 arr[0] = l;arr[1] = i;arr[2] = r;
33                 max = total;
34             }
35         }
36         return arr;
37     }
38 }

原文地址:https://www.cnblogs.com/messi2017/p/10055784.html

时间: 2024-08-30 15:48:27

动态规划——Maximum Sum of 3 Non-Overlapping Subarrays的相关文章

1031. Maximum Sum of Two Non-Overlapping Subarrays

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-length subarray.) Fo

[ACM] POJ 2479 Maximum sum (动态规划求不相交的两段子段和的最大值)

Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33363   Accepted: 10330 Description Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: Your task is to calculate d(A). Input The input consists o

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

[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

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] 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

[Swift]LeetCode1031. 两个非重叠子数组的最大和 | Maximum Sum of Two Non-Overlapping Subarrays

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-length subarray.) Fo

【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 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