[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, 3] and [2, -1, 2] or [1, 3, -1, 2] and [2], they both have the largest sum 7.

Note

The subarray should contain at least one number

Challenge

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

与买卖股票的思路一样,从两边分别计算最大子串

public class Solution {
    /**
     * @param nums: A list of integers
     * @return: An integer denotes the sum of max two non-overlapping subarrays
     */
    public int maxTwoSubArrays(ArrayList<Integer> nums) {
        // write your code
        if(nums.size()==0 || nums==null) return 0;
        int n=nums.size();
        int result=Integer.MIN_VALUE;;

        int[] preSum=new int[n];

        int sum1=0;
        int max=Integer.MIN_VALUE;
        for(int i=0;i<n;i++)
        {
            sum1=Math.max(sum1+nums.get(i),nums.get(i));
            max=Math.max(max,sum1);
            preSum[i]=max;
        }

        max=Integer.MIN_VALUE;
        int sum2=0;
        for(int i=n-1;i>=0;i--)
        {
            if(i<n-1)
            {
                result=Math.max(result,preSum[i]+max);
            }
            sum2=Math.max(sum2+nums.get(i),nums.get(i));
            max=Math.max(max,sum2);
        }
        return result;
    }
}
时间: 2024-10-18 07:55:14

[lintcode medium]Maximum Subarray II的相关文章

[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

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

[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

[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

【LeetCode】 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. More practice: If you have figu

LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方案

原始题目如下,意为寻找数组和最大的子串,返回这个最大和即可. 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.

41. leetcode 53. Maximum Subarray

53. 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. 思路:这个题还挺经典