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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

把“前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("----------------");

        }

[一句话思路]:

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

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

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

[总结]:

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

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

[关键模板化代码]:

[其他解法]:

[Follow Up]:

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

[代码风格] :

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

原文地址:https://www.cnblogs.com/immiao0319/p/9102541.html

时间: 2024-10-07 21:43:32

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

689. Maximum Sum of 3 Non-Overlapping Subarrays

Max Sum of Subarray with size k, 相当于Easy难度,我说用一个sum array存sum,然后做减法就行.中国小哥说让优化空间,于是说可以只用两个数. 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 t

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

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

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

Poj 2479 Maximum sum【双向DP/最大连续和】

Maximum sum 题意:给定一个长度为N的数组,求两个连续的子序列,使得两个连续子序列的和最大. 分析:乍一看,跟最大连续和有点类似,但是,又有区别,因为对于这个题,考虑第i项两个连续子序列的最大和,不能仅仅由前i-1项递推得出,第i项两个连续子序列的最大和,与前i项和i以后的之间是存在关系的,因此这个题目是一个双向dp. 假如给定的序列为a0, a1, a2, a3, a4, ...... ,an,那么,对于任意第i项,我以第i个元素为分界点,用一个数组项pMax [i]来保存  区间

[LeetCode] 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 p

[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 (contiguous) subarrays, which have lengths L and M.  (For clarification, the L-length subarray could occur before or after the M-length subarray.) Fo

Leetcode 1031 Maximum Sum of Two Non-Overlapping Subarrays (滑动窗口)

Leetcode 1031 题目描述 Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L and M. (For clarification, the L-length subarray could occur before or after the M-le

1031. 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 (contiguous) subarrays, which have lengths L and M.  (For clarification, the L-length subarray could occur before or after the M-length subarray.) Fo