689. 三个无重叠子数组的最大和

给定数组 nums 由正整数组成,找到三个互不重叠的子数组的最大和。

每个子数组的长度为k,我们要使这3*k个项的和最大化。

返回每个区间起始索引的列表(索引从 0 开始)。如果有多个结果,返回字典序最小的一个。

示例:

输入: [1,2,1,2,6,7,5,1], 2

输出: [0, 3, 5]

解释: 子数组 [1, 2], [2, 6], [7, 5] 对应的起始索引为 [0, 3, 5]。

我们也可以取 [2, 1], 但是结果 [1, 3, 5] 在字典序上更大。

注意:

nums.length的范围在[1, 20000]之间。

nums[i]的范围在[1, 65535]之间。

k的范围在[1, floor(nums.length / 3)]之间。

class Solution {
private int maxSum; public int[] maxSumOfThreeSubarrays(int[] nums, int k) { int n = nums.length; int[] sum = new int[n+1], left = new int[n], right = new int[n]; for(int i=0; i<n; i++){
sum[i+1] = sum[i]+nums[i];
}
//从左面筛选
for(int i=k, leftmax = sum[k]-sum[0]; i<n ;i++){
if(sum[i+1]-sum[i+1-k] > leftmax){
leftmax = sum[i+1] - sum[i+1-k];
left[i] = i+1-k;
}else{
left[i] = left[i-1];
}
}
//右面筛选
right[n-k] = n-k;
for(int i=n-k-1, rightMax = sum[n]-sum[n-k]; i>=0; i--){
if(sum[i+k]-sum[i]>= rightMax){
right[i] = i;
rightMax = sum[i+k] - sum[i];
}else{
right[i] = right[i+1];
}
}
//去中间找,然后记录总和
int maxsum = 0; int[] result = new int[3];
for(int i=k; i<=n-2*k; i++){
int l = left[i-1], r = right[i+k];
int total = (sum[i+k]-sum[i]) + (sum[l+k] - sum[l]) + (sum[r+k]-sum[r]);
if(total>maxsum){
maxsum = total;
result[0] = l; result[1] = i; result[2] =r;
}
} return result; }
}

Java实现 LeetCode 689 三个无重叠子数组的最大和(换方向筛选)的更多相关文章

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

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

  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 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. Java实现 LeetCode 713 乘积小于K的子数组(子集数量+双指针)

    713. 乘积小于K的子数组 给定一个正整数数组 nums. 找出该数组内乘积小于 k 的连续的子数组的个数. 示例 1: 输入: nums = [10,5,2,6], k = 100 输出: 8 解 ...

  7. Java实现 LeetCode 560 和为K的子数组(某著名排序大法改编)

    560. 和为K的子数组 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] ...

  8. 剑指offer三十之连续子数组的最大和

    一.题目 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...

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

随机推荐

  1. 一步步打造QQ群发消息群发器

    最近为了做公众号号推广,吸粉,然后加了几百个QQ群,感觉QQ群的群发效果还是不错的,一天能捞到100个粉丝左右,好的时候也有200个,少的时候几十个,但是由于太多的群了,手工一个个点击开来群发,几百个 ...

  2. 你的团队需要一个正确的程序集(dll)管理姿势

    很多团队经历时间的积淀之后,都会有很多的可重用的公共技术组件.大部分的团队都会把这些公共组件生成程序集(dll)后,放到GIT或SVN的一个公共目录里面,以供各个项目中使用.起初在项目很少又或者是公共 ...

  3. SDK,JDK,API的区别

    [基础概念] 先留一波传送门: SDK:软件开发工具包(外语全称:Software Development Kit)一般都是一些软件工程师为特定的软件包.软件框架.硬件平台.操作系统等建立应用软件时的 ...

  4. mybatis-generator生成的mapper中的

    int updateByExampleSelective(@Param("record") Shop record, @Param("example") Sho ...

  5. android 数据库是否该关闭

    关于android多线程数据读写请看博客: android 多线程数据库读写 常常纠结于获取了SQLiteDatabase每次操作完是否要关闭的问题,每次关闭又怕影响性能,这里记录下SQLiteOpe ...

  6. meta标签设置不缓存

    平常调试的时候总是因为缓存问题有些浪费时间,加上这几行代码就ok了 <meta http-equiv="Cache-Control" content="no-cac ...

  7. 阿里云wordpress轻量应用服务器升级php版本

    目录 脚本升级 php.ini没有加载 升级完后只能最大只能上传2m的文件的问题 脚本升级 用大佬写的脚本: https://yq.aliyun.com/articles/717769?spm=a2c ...

  8. 运行web容器实例

  9. 第三篇:ASR(Automatic Speech Recognition)语音识别

    ASR(Automatic Speech Recognition)语音识别: 百度语音--语音识别--python SDK文档: https://ai.baidu.com/docs#/ASR-Onli ...

  10. 使用urllib

    urlopen的基本用法: 工具为:python3(windows) 其完整表达式为: urllib.request.urlopen(url, data=None, [timeout, ]*, caf ...