[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

note中已经提示了length,就只需要考虑k k&length的关系就了

把“前i项”初始化为“第i项”,方便直接做差

for (int i = 1; i <= n; i++) {
sums[i] = sums[i - 1] + nums[i - 1];
}

[思维问题]:

不知道为什么要用DP:每次都保存之前一组的状态,然后一个个向前更新和比价。

求一组固定为k长度的数组时可用。

//总和=本组和+之前组的和=本组最后之和-本组第一之和+之前的(从j - k开始的)dp求和值
int curSum = sums[j] - sums[j - k] + dp[i - 1][j - k];

[英文数据结构或算法,为什么不用别的数据结构或算法]:

dp数组里存储了结果,可以通过不断输入index来把结果取出来:

int index = n;
for (int i = 2; i >= 0; i--) {
res[i] = pos[i + 1][index];
System.out.println("index = " +index);
System.out.println("res[i] = pos[i + 1][index] = " +res[i]); index = res[i];
System.out.println("index = " +index);
System.out.println("----------------"); }

[一句话思路]:

按照第123组来操作,

 总和=本组和+之前所有组的和

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 序列型dp所有的有关数组、有关二维数组都要增加1个单位,调用的时候也要+1,因为第一位拿来初始化了。不初始化就是默认为0

[二刷]:

  1. 发现把第0位给去掉了 不知道为何:
[1,2,1,2,6,7,5,1]
2 i = 1
i - 1 = 0
nums[i - 1] = 1
sum[i - 1] = 0
sum[i - 1] = 1
---------------
i = 2
i - 1 = 1
nums[i - 1] = 2
sum[i - 1] = 0
sum[i - 1] = 2
---------------
i = 3
i - 1 = 2
nums[i - 1] = 1
sum[i - 1] = 0
sum[i - 1] = 1
---------------
i = 4
i - 1 = 3
nums[i - 1] = 2
sum[i - 1] = 0
sum[i - 1] = 2
---------------
i = 5
i - 1 = 4
nums[i - 1] = 6
sum[i - 1] = 0
sum[i - 1] = 6
---------------
i = 6
i - 1 = 5
nums[i - 1] = 7
sum[i - 1] = 0
sum[i - 1] = 7
---------------
i = 7
i - 1 = 6
nums[i - 1] = 5
sum[i - 1] = 0
sum[i - 1] = 5
---------------
i = 8
i - 1 = 7
nums[i - 1] = 1
sum[i - 1] = 0
sum[i - 1] = 1
---------------

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

dp是存储一组状态的,可以拿来调用

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

class Solution {
public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
//ini: res[3], pos[4][n + 1], dp[4][n + 1]
int n = nums.length;
int[] res = new int[3];
int[] sum = new int[n + 1];
int[][] pos = new int[4][n + 1];
int[][] dp = new int[4][n + 1]; //cc
if (nums == null || nums.length < 3 * k) return res; //ini:sum
for (int i = 1; i <= n; i++) {
int j = i - 1;
System.out.println("i = "+i);
System.out.println("i - 1 = "+j);
System.out.println("nums[i - 1] = "+nums[i - 1]);
System.out.println("sum[i - 1] = "+sum[i - 1]); sum[i - 1] = sum[i - 1] + nums[i - 1]; System.out.println("sum[i - 1] = "+sum[i - 1]);
System.out.println("---------------");
} for (int i = 1; i <= 3; i++) {
for (int j = k * i; j <= n; j++) {
int curSum = sum[j] - sum[j - k] + dp[i - 1][j - k];
if (curSum > dp[i][j - 1]) {
dp[i][j] = curSum;
pos[i][j] = j - k;
}else {
dp[i][j] = dp[i][j - 1];
pos[i][j] = pos[i][j - 1];
}
}
} //retrieve the answer
int index = n;
for (int i = 2; i >= 0; i--) {
//
res[i] = pos[i + 1][index];
index = res[i];
}
//return
return res;
}
}

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

    原题链接在这里:https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/ 题目: In a given arr ...

  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. 【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays

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

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

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

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

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

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

随机推荐

  1. CentOS6.5安装中文支持

    本人在安装CentOS6.5时选择是英文版,安装后打开文档,发现好些文档成了乱码了. 这个问题的原因是没有中文支持. 解决方法: 1.安装中文支持包 # yum groupinstall " ...

  2. Python——深浅Copy

    1. 赋值 赋值:指向同一块内存地址,所以同时改变 l1 = [1,2,3] l2 = l1 l1.append('a') print(l1,l2) # [1, 2, 3, 'a'] [1, 2, 3 ...

  3. RK3288 HDMI配置和调试

    RK3288 最大输出分辨率为 3840x2160 HDMI 驱动代码位于 kernel/drivers/video/rockchip/hdmi/rockchip-hdmiv2 目录 1.设置默认输出 ...

  4. HTML第一讲

    HTML标记区分 HTML即超文本标记语言(HtyperText Markup Language),其作用就是将编辑的内容在屏幕上显示.文件的后缀为.HTML. 在HTML中成对出现的叫做双标记(譬如 ...

  5. eclipse安装freemarker插件【转】

    今天在Eclipse上安装Freemarker的插件,一开始装官方网站上的推荐插件,装上后发现除了Freemarker代码高亮显示其他什么效果都没有,郁闷.在javaeye论坛上请教了下,据说官网上的 ...

  6. 使用BeanShell 对比取出来的值

    描述: 使用BeanShell 对比取出来的值,如不一致,报错 步骤一: 使用json Extractor后置处理器,取出"登入成功" 使用BeanS hell断言: 语法: if ...

  7. python实战——文本挖掘+xgboost预测+数据处理+准确度计算整合版

    if __name__=="__main__": '''============================先导入数据============================= ...

  8. IJ

    ylbtech-IJ: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   7.返回顶部   8.返回顶部   9.返回顶部   10.返回 ...

  9. Docker - 使用Swarm和compose部署服务(containers)

    前言 在之前使用Docker的过程中,一直是用 Docker run 命令单独启动container后再加入Overlay网络的方式实现部署工作的. 这种方式看似直接,但是随着服务所包含的contai ...

  10. 上传图片demo

    页面: js: 后台: @RequiresPermissions("pointwall:upload:edit") @RequestMapping(value = "sa ...