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. MySQL5.7.10配置和使用

    前两天搞mybatis和springmvc的结合,搞了半天项目就是跑不起来,由于都没有接触过,也不知道原因出在哪里.今天想彻底学习下mybatis,于是就想使用MySQL.从官网上下了,MySQL-5 ...

  2. 使用bcp工具对boost库裁剪

    有些时候,我们需要通过源代码来发布我们的产品,在使用了CI工具之后,一般我们要求每天对源码进行构建,以防止代码不可用了还不自知.如果我们使用了Boost库,我们就需要在构建的过程中将Boost同时构建 ...

  3. 765A Neverending competitions

    A. Neverending competitions time limit per test 2 seconds memory limit per test 512 megabytes input ...

  4. 小程序嵌套h5

    <web-view src="https://m.boc7.com/driver_unlogin/driver1"></web-view>

  5. wdk Windows驱动开发

    https://www.cnblogs.com/liaoguifa/p/9049859.html 安装wdk8.1

  6. 模型参数_grid

    from sklearn import datasetsfrom sklearn.model_selection import train_test_splitfrom sklearn.preproc ...

  7. 多线程的使用:只能用cmd来玩不能用idle

    from multiprocessing import Process import os def pro_do(name, func): print () if __name__ == " ...

  8. servlet填充Response时,数据转换之content-type

    在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值. 1. ...

  9. AJAX学习必备三本书

    <AJAX基础教程>AJAX必备图书之一.国内发行的第一本AJAX图书,也是目前最好的AJAX入门书,如果您是AJAX新手,此书是最好的入门图书.本书基本包括了实现Ajax需要了解的大部分 ...

  10. [转载][翻译]Go的50坑:新Golang开发者要注意的陷阱、技巧和常见错误[2]

    Golang作为一个略古怪而新的语言,有自己一套特色和哲学.从其他语言转来的开发者在刚接触到的时候往往大吃苦头,我也不例外.这篇文章很细致地介绍了Golang的一些常见坑点,读完全篇中枪好多次.故将其 ...