原题链接在这里: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:

 class Solution {
public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
int [] res = new int[3];
Arrays.fill(res, -1);
if(nums == null || nums.length < 3 * k){
return res;
} int n = nums.length;
int [] sum = new int[n+1];
for(int i = 0; i<n; i++){
sum[i+1] = sum[i] + nums[i];
} int [] leftPo = new int[n];
for(int i = k, max = sum[k] - sum[0]; i<n; i++){
if(sum[i+1] - sum[i+1-k] > max){
max = sum[i+1] - sum[i+1-k];
leftPo[i] = i+1-k;
}else{
leftPo[i] = leftPo[i-1];
}
} int [] rightPo = new int[n];
rightPo[n-k] = n-k;
for(int i = n-k-1, max = sum[n] - sum[n-k]; i>=0; i--){
if(sum[i+k] - sum[i] >= max){
max = sum[i+k] - sum[i];
rightPo[i] = i;
}else{
rightPo[i] = rightPo[i+1];
}
} for(int i = k, max = 0; i<=n-2*k; i++){
int l = leftPo[i - 1];
int r = rightPo[i + k];
if(sum[i+k] - sum[i] + sum[l+k] - sum[l] + sum[r+k] - sum[r] > max){
max = sum[i+k] - sum[i] + sum[l+k] - sum[l] + sum[r+k] - sum[r];
res[0] = l;
res[1] = i;
res[2] = r;
}
} return res;
}
}

类似Best Time to Buy and Sell Stock III.

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

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

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

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

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

  6. [LeetCode] 918. Maximum Sum Circular Subarray 环形子数组的最大和

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

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

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

  8. Leetcode Week5 Maximum Sum Circular Subarray

    Question Given a circular array C of integers represented by A, find the maximum possible sum of a n ...

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

随机推荐

  1. 适配器(Adapter)模式

    适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 适配器模式的一些其他名称:变压器模式.转换器模式.包装(Wrapper)模式.适 ...

  2. C语言函数调用

    1.backtrace一些内存检测工具如Valgrind,调试工具如GDB,可以查看程序运行时函数调用的堆栈信息,有时候在分析程序时要获得堆栈信息,借助于backtrace是很有帮助的,其原型如下: ...

  3. 一个刚入行的BIOS工程师的自我简介

    现在是北京时间2019年11月28日,大学毕业已经工作四个多月.说来也是奇怪,大学里面明明主修机械电子工程,几乎是纯机械方向,毕业之后的工作却与主修的课程毫无关系.因为对机械这一行业毫无兴趣,大学里面 ...

  4. 消除VS中动态申请二维数组C6011,C6385,C6386的警告

    动态申请二维数组,无非就是通过指针来实现.@wowpH 过程分三步:1.申请内存,2.使用数组,3.释放内存. 代码如下: /************************************* ...

  5. 13. Scala函数式编程(高级部分)

    13.1 偏函数(partial function) 13.1.1 需求 -> 思考 一个集合val list = List(1,2,3,4,"abc"),完成如下要求 1) ...

  6. NOI2019网络同步赛游记

    我发的邮件**f没收到,后来去专门询问才整到一个名额(估计是嫌我太菜,参加了也是垫底) day -1 上午写了到类似随机游走的高斯消元期望dp,然后颓颓颓 下午打洛咕月赛.T1一直50pts,后来才知 ...

  7. BZOJ3926 ZJOI2015诸神眷顾的幻想乡(广义后缀自动机)

    对多串建立SAM的一种方法是建trie再对trie建SAM.构造方式分为在线(也即不建trie而是依次插入每个串,或在trie上dfs)和离线(也即建好trie再bfs).其中离线构造与单串的构造方式 ...

  8. C# vb .NET读取识别条形码线性条码ean-8

    ean-8是比较常见的条形码编码规则类型的一种.如何在C#,vb等.NET平台语言里实现快速准确读取该类型条形码呢?答案是使用SharpBarcode! SharpBarcode是C#快速高效.准确的 ...

  9. 前端1-----CSS颜色属性,字体文本和背景属性,边框属性,margin和padding,盒模型,行内块转换,浮动,三大定位

    前端1-----CSS颜色属性,字体文本和背景属性,边框属性,margin和padding,盒模型,行内块转换,浮动,三大定位 一丶css选择器的优先级 行内 > id选择器 > 类选择器 ...

  10. IDEA 环境下更改Maven的仓库镜像提高下载速度

    Maven把所有常用的jar包存放在一个集中的仓库(repository)中,项目需要什么jar包和他相关的依赖,只要在pom.xml文件中声明就可了,还是很方便的.repository分两种,一个是 ...