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

Note
The subarray should contain at least one number

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

Challenge
Can you do it in time complexity O(n) ?

思路:把数组分成两部分,可以从i和i+1(0<=  i < len-1)之间分开,a[0, i] a[i+1, len-1],然后分别求两个子数组中的最大子段和,然后求和的最大值即可。

 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         if (nums==null || nums.size()==0) return 0;
 9         int len = nums.size();
10         int lLocal = nums.get(0);
11         int[] lGlobal = new int[len];
12         lGlobal[0] = lLocal;
13         for (int i=1; i<len; i++) {
14             lLocal = Math.max(lLocal+nums.get(i), nums.get(i));
15             lGlobal[i] = Math.max(lLocal, lGlobal[i-1]);
16         }
17
18         int rLocal = nums.get(len-1);
19         int[] rGlobal = new int[len];
20         rGlobal[len-1] = rLocal;
21         for (int i=len-2; i>=0; i--) {
22             rLocal = Math.max(rLocal+nums.get(i), nums.get(i));
23             rGlobal[i] = Math.max(rLocal, rGlobal[i+1]);
24         }
25
26         int res = Integer.MIN_VALUE;
27         for (int k=0; k<len-1; k++) {
28             if (res < lGlobal[k]+rGlobal[k+1])
29                 res = lGlobal[k] + rGlobal[k+1];
30         }
31         return res;
32     }
33 }
时间: 2024-08-06 07:53:26

Lintcode: Maximum Subarray II的相关文章

[lintcode medium]Maximum Subarray II

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. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,

Lintcode42 Maximum Subarray II solution 题解

[题目描述] 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. Notice:The subarray should contain at least one number 给定一个整数数组,找出两个 不重叠 子数组使得它们

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. Notice The subarray should contain at least one number For given [1, 3, -1, 2, -1, 2]

Lintcode: Maximum Subarray Difference

Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is the largest. Return the largest difference. Note The subarray should contain at least one number Example For [1, 2, -3, 1], return 6 Challenge O(n) t

Lintcode - Maximum Subarray III

Given an array of integers and a number k, find k non-overlappingsubarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. http://www.lintcode.com/en/problem/maximum-subarray-iii/ Thought: 一开始以为这和

[LintCode] Maximum Subarray 最大子数组

Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarray should contain at least one number. Have you met this question in a real interview? Yes Example Given the array [−2,2,−3,4,−1,2,1,−5,3], the contigu

[Lintcode] Best Time to Buy and Sell Stock I, II, III &amp;&amp; Maximum Subarray

买卖股票系列 && Subarray 相关题目: Best Time to Buy and Sell Stock I && II && III (IV 单独开贴) Maximum Subarray I && II 为什么这些我要总结到一起呢?因为思路基本一致. 题目简略: 买卖股票1: 一次买卖,利润最大. 买卖股票2: N次买卖,利润最大. 买卖股票3: 2次不重叠的买卖,利润最大. Maximum Subarray: Given an a

[lintcode medium]Maximum Subarray Difference

Maximum Subarray Difference Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is the largest. Return the largest difference. Example For [1, 2, -3, 1], return 6. Note The subarray should contain at leas

[LeetCode][JavaScript]Maximum Subarray

Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6. https://leetcod