#42 Maximum Subarray II

原创 2016年08月29日 03:11:50

题目描述:

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

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) ?

题目思路:

这题基于#41的基础上有所扩展:可以用#41的算法,对于每个i,都算出它左边subarray中的最大值和右边subarray中的最大值。然后再重新遍历一遍原来的数组,找到所有的i中两边最大值相加最大就可以了。

Mycode(AC = 33ms):

class Solution {
public:
    /**
     * @param nums: A list of integers
     * @return: An integer denotes the sum of max two non-overlapping subarrays
     */
    int maxTwoSubArrays(vector<int> nums) {
        // write your code here
        if (nums.size() <= 1) return 0;
        
        vector<int> left_max(nums), right_max(nums);
        
        // get max for subarray indexed 0...i
        int local = nums[0];
        for (int i = 1; i < nums.size(); i++) {
            local = max(local + nums[i], nums[i]);
            left_max[i] = max(left_max[i - 1], local);
        }
        
        // get max for subarray indexed i...nums.size() - 1
        local = nums[nums.size() - 1];
        for (int i = nums.size() - 2; i >= 0; i--) {
            local = max(local + nums[i], nums[i]);
            right_max[i] = max(right_max[i + 1], local);
        }
        
        // get final result
        int global = INT_MIN;
        for (int i = 1; i < nums.size(); i++) {
            global = max(global, left_max[i - 1] + right_max[i]);
        }
        return global;
    }
};


版权声明:三千微尘里,吾宁爱与憎

相关文章推荐

Lintcode42 Maximum Subarray II solution 题解

【题目描述】 Given an array of integers, find two non-overlapping subarrays which have the largest su...

Leetcode 644. Maximum Average Subarray II 最大平均区间2 解题报告

这道题目呢,和之前的643类型差不多,但是更难。 首先这道题我看了Solution的解法,反正不是n2的复杂度,是 n*log(max-min) 的样子吧,一开始怎么做都不对,超时。。。直到直接跑了...
  • MebiuW
  • MebiuW
  • 2017-07-27 22:41
  • 1027

Array(2) -- Pascal's Triangle II,Maximum Product Subarray,Word Search,Majority Element II, S R

Pascal's Triangle II,Maximum Product Subarray,Word Search,Majority Element II,Summary Ranges

Array(6) -- Maximum Subarray,Rotate Image,Unique Paths II,Spiral Matrix I, II

Maximum Subarray,Rotate Image,Unique Paths II,Spiral Matrix,Spiral Matrix II

Maximum Subarray-最大连续子数组和

Maximum Subarray

[LeetCode]Maximum Subarray

Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of ...

LeetCode(53) Maximum Subarray

题目Find the contiguous subarray within an array (containing at least one number) which has the larges...

LeetCode—Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest ...

[leetcode]Maximum subarray

Maximum subarray是一个很常见的DP问题

leetcode刷题之旅——53. Maximum Subarray

本周写的第二道分治算法的题目,因为上次选择的是一道easy的题目,为了提升自己,这次选择了一道middle难度的题目。虽然这道题目是middle的,但是我还是想了很长时间。其实这道题目可以用动态规划直...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)