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.

解法:动归。对于每个element i, 分别记录 [0, i ] 的left maxsum和(i, n)的right maxsum,最后扫描一遍记录,找到left + right最大的sum.

注意:因为subarray最少也要包含一个element,且subarray不应该overlap, 把left[i] 定位为结尾到 i 中的 maxsum(这个subarray可能包含i,也可能不包含i), right[i] 代表以i+1开头 往后中的maxsum.

算法公式:
对于left[i]来说,如果包含 i,最大值是Math.max( left[i-1] + nums[i],nums[i] ); 如果不包含 i, 最大值是left[i-1];
对于right[i]来说,如果包含i+1, 最大值是Math.max(right[i+1] + nums[i+1], nums[i+1]), 如果不含i+1, 最大值是right[i+1];

    public int maxTwoSubArrays(ArrayList<Integer> nums) {
        int res = Integer.MIN_VALUE;
        if (nums.size() == 0) return res;
        // calculate from left and record max
        int[] left = new int[nums.size()];
        // localmax is subarray ends in i;
        int localmax = nums.get(0);
        // globalmax is subarray within 0~i;
        int globalmax = Integer.MIN_VALUE;
        left[0] = nums.get(0);
        for (int i = 1; i < nums.size(); i++) {
            localmax = Math.max(localmax + nums.get(i), nums.get(i));
            globalmax = Math.max(localmax, globalmax);
            left[i] = globalmax;
        }
        // right local max is subarray ends in i+1
        localmax = 0;
        globalmax = Integer.MIN_VALUE;
        for (int i = nums.size()-2; i >= 0; i--) {
            localmax = Math.max(localmax + nums.get(i+1), nums.get(i+1));
            globalmax = Math.max(localmax, globalmax);
            res = Math.max(res, globalmax+left[i]);
        }
        return res;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值