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 ...
随机推荐
- golang知识精要(一)
一.第一章 命令行参数可通过os.Args访问,os.Args是切片 切片遵循左闭右开原则,如sl[1:3]不包含下标为3的元素 for循环两种方式 方式一: for initial; conditi ...
- html 显示 pdf
html 显示 pdf文件四种方式: 1. <embed src="pdf/wobu.pdf" type="application/pdf" width= ...
- azure跨域问题(访问azure存储账户数据,blob)
访问azure存储账户数据报错:405错误 解决方案 打开访问的存储账户--->CORS--->Blob服务 全部填写*就可以了,点击“保存”即可. iframe就可以展示blob中的pd ...
- android.view.ViewRoot$CalledFromWrongThreadException 异常的解决方案
https://blog.csdn.net/vincent_czz/article/details/7070354 https://stackoverflow.com/questions/210141 ...
- sparkContext的初始化过程
SparkContext 初始化的过程主要的核心:1) 依据 SparkContext 的构造方法中的参数 SparkConf 创建一个SparkEnv2) 初始化,Spark UI,以便 Spark ...
- TP5.0使用助手函数model出现\common\Model\类不存在
在ThinkPHP5.0中有一个助手助手函数model(),可以实例化具体的模型,包括分层模型,只要传入类名(第一个参数),分层名(第二个参数).这个函数其实是ThinkPHP框架Loader中的一个 ...
- selenium登录简单的网站
import time from selenium import webdriver from lxml import etree from selenium.webdriver import Act ...
- redis中获取不同自增数的方法
项目需求,需要获取不同的自增数,然后与其他信息拼接成一个字符串作为编号,这边有一种基于数据库的获取自增数的方法,这边略过,还有一种基于redis的实现. 此方法可以用到redis的自增函数 publi ...
- MySQL倒序索引测试1
测试环境 MySQL Community Server 准备测试数据 DROP TABLE TB001; CREATE TABLE TB001(ID INT PRIMARY KEY AUTO_INCR ...
- document.forms使用
定义:document.forms返回form表单的集合,包含了当前DOM结构中所有的form表单. 语法: . 获取当前DOM结构中的第一个form表单. document.forms[] . 获取 ...