L42 Maximum Subarray II
Last updated
Was this helpful?
Last updated
Was this helpful?
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.
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 sum7
.
Can you do it in time complexity O(n) ?
这题左边来一次,右边来一次,最后因为两个subarray不能重叠,所以最后的loop要错开一个位置