-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42.h
More file actions
36 lines (32 loc) · 1.03 KB
/
42.h
File metadata and controls
36 lines (32 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
int lmax[nums.size()];
int rmax[nums.size()];
lmax[0] = nums[0];
rmax[nums.size()-1] = nums[nums.size()-1];
int cur = nums[0];
for(int i = 1; i < nums.size(); i++){
if(cur < 0) cur = nums[i];
else cur += nums[i];
lmax[i] = max(lmax[i-1], cur);
}
cur = nums[nums.size()-1];
for(int i = nums.size()-2; i >= 0; i--){
if(cur < 0) cur = nums[i];
else cur += nums[i];
rmax[i] = max(rmax[i+1], cur);
}
int ret = INT_MIN;
// 到最后一个元素前位置,保证右端区间至少有一个元素
for(int i = 0; i < nums.size()-1; i++){
ret = max(lmax[i]+rmax[i+1], ret);
}
return ret;
}
};