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 ...
随机推荐
- myeclipse导入项目中文乱码怎么解决教程
大家在Myeclipse导入项目的时候,应该都遇见过一些乱码的问题,不单单只是Myeclipse有这个问题,那么怎么解决Myeclipse导入项目乱码的问题呢,问题出现的原因是什么呢,下面来看看答案. ...
- python3 根据时间获取本月一号和月末日期
一.概述 有一个统计报表需求,需要知道上个月的第一天和最后一天,来进行上个月的数据统计. 二.代码实现 #!/usr/bin/env python3 # coding: utf-8 import ca ...
- SQL分类之DCL:管理用户、授权
DCL:管理用户.授权 SQL分类: DDL:操作数据库和表 DML:增删改表中的数据 DQL:查询表中的数据 DCL:管理用户.授权 DBA:数据库管理员 DCL:管理用户.授权 1.管理用户 1. ...
- Mybatis+MySql 一个标签中执行多条sql语句
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/cxfly957/article/details/77896590 MySql默认是不支持这种骚操作的 ...
- WPF USB设备采集开源工具介绍
最近项目需要需要试试采集电脑USB 设备信息 找到了 开源 示例 代码非常好 公共库也很友好 可能知名度太低 才4star https://github.com/vurdalakov/usbd ...
- C# GDI graphics.DrawImage 的参数问题
graphics.DrawImage(imageSource, new System.Drawing.Point[] { ,), , ), , ), } ); graphics.DrawImage(i ...
- C# 特性和索引(C#学习笔记06)
特性 特性(Attribute)是用于在运行时传递程序中各种元素(比如类.方法.结构.枚举.组件等)的行为信息的声明性标签. 特性可以当成一个特殊的类看待 列举特性语法: [attribute(pos ...
- 【转载】 Windows系统电脑通过设备管理器查看电脑配置信息
在采购电脑或者使用电脑的过程中,有时候我们需要查看到电脑的所有设备硬件信息,此时就可以通过Windows系统自带的设备管理器界面来查看该电脑所有的设备配置信息,包括CPU型号频率.内存.硬盘型号以及容 ...
- JavaScript数据类型和语法
第一章 类型 1.2 内置类型 使用 typeof 检测对象类型并不是非常安全的行为: // 安全的 typeof undefined // 'undefined' typeof true // 'b ...
- 剑指前端(前端入门笔记系列)——DOM(基本组成与操作)
DOM——基本组成与操作 DOM是针对HTML和XML文档的一个API(应用程序编程接口).DOM描绘了一个层次化的节点树,允许开发人员添加.移除和修改页面的某一部分.他给文档提供了一种结构化的表达方 ...