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. zabbix批量添加SNMP监听H3C端口检测以及流量图

    由于之前网络设备不是很多,监控网络设备接口就直接使用模版中的item来实现了,可是现在公司上线了一大批网络设备,如果要每个网络设备都做模板,添加item......那就该废了,于是迫于压力今天来测试使 ...

  2. Docker集群管理(二)—— docker+swarm+etcd+shipyard

    引言 前一篇介绍如何简单的搭建一个可视化管理的docker集群,本篇将在此基础之上引入etcd发现服务. 目的 使用etcd发现服务解决swarm内置发现服务的不稳定问题.etcd采用raft算法,这 ...

  3. linux-centos6/7初始配置

    关闭防火墙 chkconfig iptables off centos7下的命令为 systemctl stop firewalld.service #停止Firewall systemctl dis ...

  4. js 解决图片居中问题

    下述方法能够解决图片居中问题: (1)宽一些或者高一些(相对父元素的大小):图片在父元素的可视范围内显示图片的中间位置 (2)小一些(相对父元素的大小):图片在父元素的可视范围内居中显示 实现原理:根 ...

  5. django中的 form 表单操作

     form组件  1. 能做什么事?   1. 能生成HTML代码  input框   2. 可以校验数据   3. 保留输入的数据   4. 有错误的提示   1. 定义   from django ...

  6. 经典算法 Morris遍历

    内容: 1.什么是morris遍历 2.morris遍历规则与过程 3.先序及中序 4.后序 5.morris遍历时间复杂度分析 1.什么是morris遍历 关于二叉树先序.中序.后序遍历的递归和非递 ...

  7. 2018-2019-2 《网络对抗技术》Exp5 MSF基础应用 Week7-8 20165233

    Exp5 MSF基础应用 目录 一.基础问题 二.攻击实例 主动攻击 ms08_067_netapi(成功) ms10_061_spoolss(失败) 针对浏览器的攻击 ms14_064_ole_co ...

  8. rsync同步web数据

    rsync远程同步web服务器的数据 实验拓扑                                            服务器A(rsync服务器)--------------服务器B( ...

  9. 练手nginx反向代理和负载均衡apache实战

    先说下原理性的 什么是反向代理 用户访问域名  域名的指向到nginx  nginx把请求转发到apache  apache处理后 返回给用户 整套的逻辑 对于用户来说  就是访问域名 然后返回  没 ...

  10. 9. MyEclipse中的SVN操作手册

     该文章转载出处:http://blog.sina.com.cn/s/blog_8a3d83320100zhmp.html 1.导入项目 点击工具栏上的[File-Import],进入下图 (如果你的 ...