原题链接在这里: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的更多相关文章

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

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

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

  4. 【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays

    题目如下: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...

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

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

  7. 689. Maximum Sum of 3 Non-Overlapping Subarrays三个不重合数组的求和最大值

    [抄题]: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...

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

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

随机推荐

  1. <面试题分享> 记两次58面试

    说明 来北京找工作,有个猎头看我的简历不错,帮我投了两个58同城的面试,投的都比较高,题也注重原理,较难,这里分享出来,给有需要的人和自己提个醒,保持空杯 面试题内容 2019.05.07 北京58企 ...

  2. Oracle查询所有字段另加两个拼接字段的操作

    Oracle查询所有字段,再加两个字段拼接, select a.*,(SNO||SNAME) from TEST_STUDENT a; 同理,查询所有字段,其中两个字段求和:(SNO和SAGE都是NU ...

  3. 从nsurlsession、Alamofire到moya

    更好的理解(抽象).更少的构建(配置).更方便的表达(语言) 一.iOS系统的网络编程(DSL概念) ios缺省的网络编程只是给出了网络编程的基本概念: urlsession.request.resp ...

  4. maven添加本地包命令mvn install:install-file

    mvn install:install .jar -Dfile:要注册的jar,绝对路径

  5. K8S学习笔记之Grafana App for Kubernetes的配置

    Grafana有一套针对Kubernetes监控的APP,和Grafana-Zabbix App类似,但是配置咋一看比较麻烦,主要参数都是来自K8S. 这款APP的详细介绍请参考Grafana App ...

  6. 8、VUE自定义组件

    1.为什么要使用自定义组件? 自定义组件是用来封装复杂的内容,提高可重用性,比如封装复杂的表格组件.日历组件.图片轮播组件等. 2.自定义组件 2.1. 全局组件 全局组件是每个Vue对象都能使用的组 ...

  7. .net HttpClient 回传实体帮助类

    public class HttpClientHelper<T> { /// <summary> /// Get请求 返回实体 /// </summary> /// ...

  8. Java 数组(三)二维数组

    如果一维数组的各个元素仍然是一个数组,那么它就是一个二维数组.二维数组常用于表示表,表中的信息以行和列的形式组织,第一个下标代表元素所在的行,第二个下标代表所在的列. 一.二维数组的创建 1.先声明, ...

  9. AngularJS 菜鸟

    AngualrJS是 JavaScript框架. <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/ang ...

  10. 英语orientaljasper鸡血石orientaljasper单词

    鸡血石(orientaljasper),是辰砂条带的地开石,因鲜红色似鸡血的辰砂(朱砂)而得名.鸡血石含有辰砂(朱砂).石英.玉髓35%-45%.磁铁矿.赤铁矿6%-12%.辰砂约5%-8%. 鸡血石 ...