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

思路:

We need to find 3 subarrays

Let's say if I can find the 2nd subarray , then find the largest subarray on both left side and right side, problem solved.

代码:

 class Solution {
public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
int[] sum = new int[nums.length]; // sum[i] = num[i] + nums[i+1]...+nums[i+k-1];
int[] lef = new int[nums.length]; // lef[i] = before i, the max sum[];
int[] rig = new int[nums.length]; // rif[i] = after i, the max sum[];
int[] IndexL = new int[nums.length];
int[] IndexR = new int[nums.length];
int total = 0; //build sum[]
for(int i=0; i<nums.length; i++){
if(i <= k-1){
total += nums[i];
}else{
total = total + nums[i] - nums[i-k];
}
if(i-k+1>=0){
sum[i-k+1] = total;
}
} int max = 0;
//build lef[]
for(int i=0; i<=nums.length-k; i++){ //i-k+1 < nums.length -> j < n-k+1
if(sum[i] > max){
max = sum[i];
lef[i] = max;
IndexL[i] = i;
}else{
lef[i] = lef[i-1];
IndexL[i] = IndexL[i-1];
}
}
max = 0;
//build rig[]
for(int i=nums.length-k; i>=0; i--){
if(sum[i] >= max){
max = sum[i];
rig[i] = max;
IndexR[i] = i;
}else{
rig[i] = rig[i+1];
IndexR[i] = IndexR[i+1];
}
}
// find 2rd subarray;
total = 0;
int ret = 0;
int[] ans = new int[3];
for(int i=k; i<=nums.length-2*k; i++){ // since no overlap so start with k;
total = sum[i] + lef[i-k] + rig[i+k]; //i+k <= nums.length-k
if(total > ret){
ret = total;
total = 0;
ans[0] = IndexL[i-k];
ans[1] = i;
ans[2] = IndexR[i+k];
}
}
return ans;
}
}

[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] 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. Java实现 LeetCode 689 三个无重叠子数组的最大和(换方向筛选)

    689. 三个无重叠子数组的最大和 给定数组 nums 由正整数组成,找到三个互不重叠的子数组的最大和. 每个子数组的长度为k,我们要使这3*k个项的和最大化. 返回每个区间起始索引的列表(索引从 0 ...

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

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

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

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

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

  8. leetcode面试题42. 连续子数组的最大和

      总结一道leetcode上的高频题,反反复复遇到了好多次,特别适合作为一道动态规划入门题,本文将详细的从读题开始,介绍解题思路. 题目描述示例动态规划分析代码结果 题目   面试题42. 连续子数 ...

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

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

随机推荐

  1. ROS-RouterOS 的license注册级别

    原文: https://wiki.mikrotik.com/wiki/Manual:CHR#CHR_Licensing https://wiki.mikrotik.com/wiki/Manual:Li ...

  2. Python XML解析

    什么是XML? XML 指可扩展标记语言(eXtensible Markup Language). 你可以通过本站学习XML教程 XML 被设计用来传输和存储数据. XML是一套定义语义标记的规则,这 ...

  3. python中__name__的使用

    这几天开始学习Python,遇到一些问题,也解决了一些问题. 其中if __name__ == '__main__':这句估计很多和我一样的初学者都是不求甚解.这里作一下解释: 1:__name__是 ...

  4. Python - Django - ORM 实例(二)

    在 app01/models.py 中添加 Book 类对象表 from django.db import models # Create your models here. # 出版社 class ...

  5. Python - Django - App 的概念

    App 方便我们在一个大的项目中,管理实现不同的业务功能 创建 App: 命令行: python manage.py startapp app名 使用 Pycharm 创建: 文件 -> 新建项 ...

  6. 解决Visual Studio “无法导入以下密钥文件”的错误

    错误1无法导入以下密钥文件: Common.pfx.该密钥文件可能受密码保护.若要更正此问题,请尝试再次导入证书,或手动将证书安装到具有以下密钥容器名称的强名称 CSP: VS_KEY_ 1110Co ...

  7. 〈Android 群英传-神兵利器〉第7章一个的寂寞与一群人的狂欢

    |---第7章一个的寂寞与一群人的狂欢 |---7.1如何解决问题 |---Chrome浏览器 |---Chrome开发者工具 |---Chrome插件(Json-Handle:Json格式化查看工具 ...

  8. overflow: auto 图片自适应调整

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Mongodb 安装 和 启动

    教程:http://www.mongodb.org.cn/tutorial/59.html 下载 >wget https://fastdl.mongodb.org/linux/mongodb-l ...

  10. python练习题:三级菜单

    需求:可依次选择进入各子菜单可从任意一层往回退到上一层可从任意一层退出程序所需新知识点:列表.字典 测试环境:win7系统,python3.7.0,工具:pycharm-community-2018. ...