Maximum Subarray II

Posted YuriFLAG

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maximum Subarray II相关的知识,希望对你有一定的参考价值。

Given an array of integers, find two non-overlapping subarrays which have the largest sum.
The number in each subarray should be contiguous.
Return the largest sum.

For given [1, 3, -1, 2, -1, 2], the two subarrays are [1, 3] and [2, -1, 2] or [1, 3, -1, 2] and [2], they both have the largest sum 7.

技术分享

思路:遍历每一个分割线,分别求分割线左边的最大子数组和分割线右边的最大子数组。为了实现O(n) 的时间复杂度,可以创建left[] 、right[] 数组,其中left[i] 表示从0 ~ i 的数组的子数组最大值,right[j] 表示nums.length - 1 ~ j 的数组的子数组最大值。

 1 public class Solution {
 2     /**
 3      * @param nums: A list of integers
 4      * @return: An integer denotes the sum of max two non-overlapping subarrays
 5      */
 6     public int maxTwoSubArrays(ArrayList<Integer> nums) {
 7         if (nums == null || nums.size() == 0) {
 8             return 0;
 9         }
10         int[] left = new int[nums.size()];
11         int[] right = new int[nums.size()];
12         int sum = 0, max = Integer.MIN_VALUE;
13         for (int i = 0; i < nums.size(); i++) {
14             sum = sum + nums.get(i);
15             max = Math.max(max, sum);
16             sum = Math.max(sum, 0);
17             left[i] = max;
18         }
19         sum = 0;
20         max = Integer.MIN_VALUE;
21         for (int i = nums.size() - 1; i >= 0; i--) {
22             sum = sum + nums.get(i);
23             max = Math.max(max, sum);
24             sum = Math.max(sum, 0);
25             right[i] = max;
26         }
27         max = Integer.MIN_VALUE;
28         for (int i = 0; i < nums.size() - 1; i++) {
29             max = Math.max(left[i] + right[i + 1], max);
30         }
31         return max;
32     }
33 }
 1 public class Solution {
 2     /**
 3      * @param nums: A list of integers
 4      * @return: An integer denotes the sum of max two non-overlapping subarrays
 5      */
 6     public int maxTwoSubArrays(ArrayList<Integer> nums) {
 7         // write your code
 8         int size = nums.size();
 9         int[] left = new int[size];
10         int[] right = new int[size];
11         int sum = 0;
12         int minSum = 0;
13         int max = Integer.MIN_VALUE;
14         for(int i = 0; i < size; i++){
15             sum += nums.get(i);
16             max = Math.max(max, sum - minSum);
17             minSum = Math.min(sum, minSum);
18             left[i] = max;
19         }
20         sum = 0;
21         minSum = 0;
22         max = Integer.MIN_VALUE;
23         for(int i = size - 1; i >= 0; i--){
24             sum += nums.get(i);
25             max = Math.max(max, sum - minSum);
26             minSum = Math.min(sum, minSum);
27             right[i] = max;
28         }
29         max = Integer.MIN_VALUE;
30         for(int i = 0; i < size - 1; i++){
31             max = Math.max(max, left[i] + right[i + 1]);
32         }
33         return max;
34     }
35 }

 

以上是关于Maximum Subarray II的主要内容,如果未能解决你的问题,请参考以下文章

lintcode-medium-Maximum Subarray II

[leetcode-644-Maximum Average Subarray II]

Lintcode42 Maximum Subarray II solution 题解

算法设计与分析[0009] Dynamic Programming(II)(Maximum Sum/Product Subarray)

Maximum Subarray

LeetCode 53. Maximum Subarray