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. 详解JMeter正则表达式

    详解JMeter正则表达式(1) 1.概览 JMeter中包含范本匹配软件Apache Jakarta ORO .在Jakarta网站上有一些关于它的文档,例如a summary of the pat ...

  2. 解决Android启动显示空白界面的问题

    Android程序启动时,第一个看的界面并不是我们的指定的第一个Activity界面,而是显示了一个空白的界面,带标题栏的,但是界面什么内容都没有,这个界面只显示不到1秒左右的时间就会切换到我们的第一 ...

  3. 3层+SVN学习笔记(2)

    在对于餐桌付款程序设计时,需要先选中餐桌,然后点击付款.正常情况是这样的: 在程序设计时,没有考虑到用户未点击餐桌而直接进行付款的情况,程序出现以下错误: 在设计时,需要考虑用户未点击餐桌而直接进行付 ...

  4. 使用Java实现网络爬虫

    网络爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本. 另外一些不常使用的名字还有蚂蚁.自动索引.模 ...

  5. 2018.09.09 poj2949Word Rings(01分数规划+spfa判环)

    传送门 这题要先巧妙的转化一下. 对于每个字符串,我们把头尾的两个小字符串对应的点连边,边权是这个字符串的长度. 这样最多会出现26*26个点. 这个时候就只用求出边权和跟边数的最大比值了. 这个显然 ...

  6. 实现字符串函数,strlen(),strcpy(),strcmp(),strcat()

    实现字符串函数,strlen(),strcpy(),strcmp(),strcat() #include<stdio.h> #include<stdlib.h> int my_ ...

  7. C++/C头文件 .h和 .c

    在C语言家族程序中,头文件被大量使用.一般而言,每个C++/C程序通常由头文件(header files)和定义文件(definition files)组成.头文件作为一种包含功能函数.数据接口声明的 ...

  8. Linux服务器部署系列之四—DHCP篇

    DHCP服务器的配置是Linux服务器配置中最简单的服务之一,网上也有很多相关文档,不过大部分都只是讲解了配置.虽然我这篇文档也不一定很完善,不过我还是希望能够尽量说得明白一些,同时也希望大家能够提供 ...

  9. 百度地图(icon,zIndex)

    百度地图v2.0参考http://lbsyun.baidu.com/cms/jsapi/reference/jsapi_reference.html#a3b2 覆盖物(Marker) Icon: va ...

  10. Python调用Google翻译

    出自:http://blog.csdn.net/zhaoyl03/article/details/8830806 最近想动手做一个文档自动下载器,需要模拟浏览器的行为.虽然感觉思路上没有困难,但在技术 ...