LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays
原题链接在这里: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的更多相关文章
- [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 ...
- [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 ...
- 【LeetCode】689. Maximum Sum of 3 Non-Overlapping Subarrays 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximum- ...
- 【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays
题目如下: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...
- 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 ...
- [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 ...
- 689. Maximum Sum of 3 Non-Overlapping Subarrays三个不重合数组的求和最大值
[抄题]: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...
- 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 ...
- [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 ...
随机推荐
- FPGA 开发板入手途径有哪些呢?
买到一块 FPGA 开发板,你如何入手呢? 根据博主的经验,你可以通过如下途径来学习: 1.如果你是淘宝上买的,那么可以在淘宝上搜索你的开发板(一般 FPGA 开发板生厂商在淘宝上卖都会附带教程,如米 ...
- Advanced-REST-client 获取及安装
作为一个java开发人员,大家或多或少的要写或者接触一些http接口.而当我们需要本地调试接口常常会因为没有一款好用的工具而烦恼.今天要给大家介绍一款非常好用.实用且方便的http接口测试工具. 获取 ...
- 第一阶段:Java基础 1.JAVA开发介绍---6. Java基本数据类型
Java 的两大数据类型: 内置数据类型(基本数据类型) 引用数据类型 本数据类型: Java语言提供了八种基本类型.六种数字类型,一种字符类型,还有一种布尔型. byte,short,int,lon ...
- Java 数组(一)定义与访问
一.数组 1.容器概述 容器:是将多个数据存储到一起,每个数据称为该容器的元素. 2.数组概述 数组:数组就是存储数据长度固定的容器,保证多个数据的数据类型要一致. 数组特点: (1)数组是一 ...
- JavaScript 数据类型(基本数据类型)
JavaScript 数据类型分为简单数据类型和复杂数据类型. 简单数据类别包括 Number.String.Boolean.Undefined 和 Null 共5种. 复杂数据类型只有一个 Obje ...
- JavaScript 之 RegExp 对象
RegExp 正则表达式对象 一.正则表达式 正则表达式:定义字符串的组成规则. 1.单个字符:[ ] 如:[a].[ab].[a-zA-Z0-9] 特殊符号代表特殊含义的单个字符: \d:单个数字字 ...
- Qt使用QPainter绘制矢量图并保存为svg文件
位图和矢量图: Bitmap: Usually a larger file size Cannot be enlarged into a higher resolution as the image ...
- Js字符串用法
js字符串整理导向图 ---欢迎收藏^ - ^
- linux基础命令学习
一 su命令 1. [yunwei@elymedia ~]$ yunwei 表示当前用户名 @elymedia 表示主机名 - 表示当前路径,涉及到当前用户的主目录(家目录) $ 表示普通用 ...
- ML- 核函数(Kernel) 的 SVM
Why 核函数 目的是为了解决线性不可分问题. 核心思想是升维. 当样本点在低维空间不能很好地分开的时候, 可以考虑将样本通过某种映射(就是左乘一个矩阵) 到高维空间中, 然后在高维空间就容易求解一个 ...