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).

Approach #1: DP. [C++]

class Solution {
public:
vector<int> maxSumOfThreeSubarrays(vector<int>& nums, int k) {
int len = nums.size();
vector<int> sum = {0}, posLeft(len, 0), posRight(len, len-k);
for (int i : nums) sum.push_back(sum.back()+i);
for (int i = k, total = sum[k] - sum[0]; i < len; ++i) {
if (sum[i+1] - sum[i+1-k] > total) {
total = sum[i+1] - sum[i+1-k];
posLeft[i] = i + 1 -k;
} else
posLeft[i] = posLeft[i-1];
} for (int i = len-k-1, total = sum[len] - sum[len-k]; i >= 0; --i) {
if (sum[i+k] - sum[i] > total) {
total = sum[i+k] - sum[i];
posRight[i] = i;
} else
posRight[i] = posRight[i+1];
} int maxsum = 0;
vector<int> ans;
for (int i = k; i <= len-2*k; ++i) {
int l = posLeft[i-1], r = posRight[i+k];
int tot = (sum[i+k] - sum[i]) + (sum[l+k] - sum[l]) + (sum[r+k] - sum[r]);
if (tot > maxsum) {
maxsum = tot;
ans = {l, i, r};
}
} return ans;
}
};

  

Analysis:

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 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], ans 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 strating index for the right interval in range [i, n-1];

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

Caution. In order to get lexicgraphical smallest order, when there are tow intervals with equal max sum, always select the left most one. So in the code. the is condition is ">=" for right interval due to backward searching, and ">" for left interval.

Reference:

https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/discuss/108231/C%2B%2BJava-DP-with-explanation-O(n)

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

  1. [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. E ...

  2. 689. Maximum Sum of 3 Non-Overlapping Subarrays三个不重合数组的求和最大值

    [抄题]: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...

  3. LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays

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

  4. [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. E ...

  5. 【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays

    题目如下: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...

  6. 【LeetCode】689. Maximum Sum of 3 Non-Overlapping Subarrays 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximum- ...

  7. [LeetCode] Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  8. [Swift]LeetCode689. 三个无重叠子数组的最大和 | Maximum Sum of 3 Non-Overlapping Subarrays

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  9. [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 ...

随机推荐

  1. 集合List与DataTable互转

    /// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list&q ...

  2. HTML5新协议介绍 WebSocket

    WebSocket protocol 是HTML5一种新的协议(protocol).它是实现了浏览器与服务器全双工通信(full-duplex). 现在,很多网站为了实现即时通讯(real-time) ...

  3. Java一个文件上传工具类

    /** * 文件上传 * * @author cary * @since 2012-12-19 下午2:22:12 */ public class FileUploader { static fina ...

  4. Linux的crontab应注意事项

    今天遇到一个问题,困扰了好久,刚开始时以为crontab定时任务配置错误,后经过验证没有错误,然后又怀疑到是不是权限问题呀?将权限跟改为root后,重新配置crontab定时任务,还是不行,真是让人气 ...

  5. oracle 表分区例子

    oracle表分区详解-一步一步教你oracle分区表详解   .创建三个不同的表空间,模拟在不同磁盘上的保存不同范围的数据    create tablespace test01 datafile ...

  6. 20155210 2016-2017-2 《Java程序设计》第7周学习总结

    20155210 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 时间的度量: GMT(Greenwich Mean Time)时间:现在不是标准时间 世界时 ...

  7. 2018.10.17 NOIP模拟 发电机(概率dp)

    传送门 考试空间开大了爆零不然只有30分爆栈? 话说这题真的坑1e7没法写dfsdfsdfs 其实很好推式子. 考虑每个点安一个发动机的概率,推一波式子做个等比数列求和什么的可以证明出来是严格的1si ...

  8. 2018.07.10NOIP模拟 Draw(容斥原理)

    Draw 题目背景 SOURCE:NOIP2016-RZZ-4 T3 题目描述 给定笛卡尔坐标系上 n 个不重复的点. 定义一个 L 形为: 一个形如 (x,y),(x+1,y)-(x+a,y),(x ...

  9. FreeTextBox备忘

    添加对4.0的dll文件引用 吧aspnet_client目录 copy到根目录下 设置文件上传目录属性ImageGalleryPath 设置相册属性到 ftb.imagegallery.aspx位置 ...

  10. 理解指令的restrict属性(转)

    restrcit属性说明 restrict: EACM中的任意一个之母.它是用来限制指令的声明格式的. E - 元素名称:<my-directive></my-directive&g ...