Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathMaximum Subarray II.java
More file actions
Latest commit
executable file
·317 lines (267 loc) · 9.3 KB
/
Copy pathMaximum Subarray II.java
File metadata and controls
executable file
·317 lines (267 loc) · 9.3 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
M
1525363049
tags: Greedy, Array, DP, SequenceDP, PreSum, Subarray
给一串数组, 找数组中间两个不交互的subarray数字之和的最大值
#### DP
- 考虑两个方向的dp[i]: 包括i在内的subarraymaxsum.
- dp[i] 的特点是: 如果上一个dp[i - 1] + nums[i - 1] 小于nums[i-1], 那么就舍弃之前, 从头再来:
- dp[i] = Math.max(dp[i - 1] + nums.get(i - 1), nums.get(i - 1));
- 缺点: 无法track全局max, 需要记录max.
- 因为我们现在要考虑从左边/右边来的所有max, 所以要记录maxLeft[] 和maxRight[]
- maxLeft[i]: 前i个元素的最大sum是多少 (不断递增); maxRight反之, 从右边向左边
- 最后比较maxLeft[i] + maxRight[i] 最大值
- Space, TimeO(n)
- Rollingarray, reducesomespace, butcannotreducemaxLeft/maxRight
#### preSum, minPreSum
- preSum是[0, i] 每个数字一次加起来的值
- 如果维持一个minPreSum, 就是记录[0, i]sum的最小值(因为有可能有负数)
- preSum - minPreSum就是在 [0, i]里, subarray的最大sum值
- 把这个最大subarraysum记录在array, left[] 里面
- right[] 是一样的道理
- enumerate一下元素的排列顺位, 最后max = Math.max(max, left[i] + right[i + 1])
```
/*
LintCode: https://lintcode.com/en/problem/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, -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) ?
Tags Expand
Greedy Enumeration Array LintCode Copyright SubArray Forward-Backward Traversal
*/
/*
Thoughts:
Similar to Maximum Subarray, from one side:
dp[i]: for first i items, the max subarray sum containing nums[i - 1]
Should process the dp from both left and right side,
with index i being the division point
Note that we need to track the max for left and right,
so we need maxLeft[], maxRight[]
*/
publicclassSolution {
/*
* @param nums: A list of integers
* @return: An integer denotes the sum of max two non-overlapping subarrays
*/
publicintmaxTwoSubArrays(List<Integer> nums) {
if (nums == null || nums.size() == 0) {
return0;
}
intn = nums.size();
int[] dpLeft = newint[n + 1];
int[] dpRight = newint[n + 1];
dpLeft[0] = 0;
dpRight[n] = 0;
int[] maxLeft = newint[n + 1];;
int[] maxRight = newint[n + 1];
maxLeft[0] = Integer.MIN_VALUE;
maxRight[n] = Integer.MIN_VALUE;
// Left
for (inti = 1; i <= n; i++) {
dpLeft[i] = Math.max(dpLeft[i - 1] + nums.get(i - 1), nums.get(i - 1));
maxLeft[i] = Math.max(maxLeft[i - 1], dpLeft[i]);
}
// Right
for (intj = n - 1; j >= 0; j--) {
dpRight[j] = Math.max(dpRight[j + 1] + nums.get(j), nums.get(j));
maxRight[j] = Math.max(maxRight[j + 1], dpRight[j]);
}
// Combine
intmax = Integer.MIN_VALUE;
for (inti = 1; i < n; i++) {
max = Math.max(max, maxLeft[i] + maxRight[i]);
}
returnmax;
}
}
// Rolling array
publicclassSolution {
/*
* @param nums: A list of integers
* @return: An integer denotes the sum of max two non-overlapping subarrays
*/
publicintmaxTwoSubArrays(List<Integer> nums) {
if (nums == null || nums.size() == 0) {
return0;
}
intn = nums.size();
int[] dpLeft = newint[2];
int[] dpRight = newint[2];
dpLeft[0] = 0;
dpRight[n % 2] = 0;
int[] maxLeft = newint[n + 1];
int[] maxRight = newint[n + 1];
maxLeft[0] = Integer.MIN_VALUE;
maxRight[n] = Integer.MIN_VALUE;
// Left
for (inti = 1; i <= n; i++) {
dpLeft[i % 2] = Math.max(dpLeft[(i - 1) % 2] + nums.get(i - 1), nums.get(i - 1));
maxLeft[i] = Math.max(maxLeft[i - 1], dpLeft[i % 2]);
}
// Right
for (intj = n - 1; j >= 0; j--) {
dpRight[j % 2] = Math.max(dpRight[(j + 1) % 2] + nums.get(j), nums.get(j));
maxRight[j] = Math.max(maxRight[j + 1], dpRight[j % 2]);
}
// Combine
intmax = Integer.MIN_VALUE;
for (inti = 1; i < n; i++) {
max = Math.max(max, maxLeft[i] + maxRight[i]);
}
returnmax;
}
}
// Futher simplify:
// use 1 for loop for both left and right
publicclassSolution {
/*
* @param nums: A list of integers
* @return: An integer denotes the sum of max two non-overlapping subarrays
*/
publicintmaxTwoSubArrays(List<Integer> nums) {
if (nums == null || nums.size() == 0) {
return0;
}
intn = nums.size();
int[] dpLeft = newint[2];
int[] dpRight = newint[2];
dpLeft[0] = 0;
dpRight[n % 2] = 0;
int[] maxLeft = newint[n + 1];;
int[] maxRight = newint[n + 1];
maxLeft[0] = Integer.MIN_VALUE;
maxRight[n] = Integer.MIN_VALUE;
for (inti = 1; i <= n; i++) {
// Left
dpLeft[i % 2] = Math.max(dpLeft[(i - 1) % 2] + nums.get(i - 1), nums.get(i - 1));
maxLeft[i] = Math.max(maxLeft[i - 1], dpLeft[i % 2]);
// Right
intj = n - i;
dpRight[j % 2] = Math.max(dpRight[(j + 1) % 2] + nums.get(j), nums.get(j));
maxRight[j] = Math.max(maxRight[j + 1], dpRight[j % 2]);
}
// Combine
intmax = Integer.MIN_VALUE;
for (inti = 1; i < n; i++) {
max = Math.max(max, maxLeft[i] + maxRight[i]);
}
returnmax;
}
}
/*
Thoughts: 11.23.2015
Similar to Maximum Subarray。 Now just try to build 2 maximum subbary, from left/right.
Meetpoint i, will give largest possible sum
*/
publicclassSolution {
/**
* @param nums: A list of integers
* @return: An integer denotes the sum of max two non-overlapping subarrays
*/
publicintmaxTwoSubArrays(ArrayList<Integer> nums) {
if (nums == null || nums.size() == 0) {
return0;
}
intpreSum = 0;
intminPreSum = 0;
intmax = Integer.MIN_VALUE;
intn = nums.size();
int[] left = newint[n];
int[] right = newint[n];
for (inti = 0; i < n; i++) {
preSum += nums.get(i);
max = Math.max(max, preSum - minPreSum);
minPreSum = Math.min(minPreSum, preSum);
left[i] = max;
}
preSum = 0;
minPreSum = 0;
max = Integer.MIN_VALUE;
for (inti = n - 1; i >= 0; i--) {
preSum += nums.get(i);
max = Math.max(max, preSum - minPreSum);
minPreSum = Math.min(minPreSum, preSum);
right[i] = max;
}
max = Integer.MIN_VALUE;
for (inti = 0; i < n - 1; i++) {
intrst = left[i] + right[i + 1];
max = Math.max(max, rst);
}
returnmax;
}
}
/*
Thinking process:
Find frontSum: largest sum from index 0 till current at each index.
Find endSum: largest sum from end(endSum.length - 1) to current at each index.
Add them up: at any point i, leftSum + rightSum = largest 2 non-overlap sum.
i
i i
i i
i i
*/
publicclassSolution {
/**
* @param nums: A list of integers
* @return: An integer denotes the sum of max two non-overlapping subarrays
*/
publicintmaxTwoSubArrays(ArrayList<Integer> nums) {
if (nums == null || nums.size() == 0) {
return0;
}
int[] frontSum = newint[nums.size()];
int[] endSum = newint[nums.size()];
intmaxSum = 0;
frontSum[0] = nums.get(0);
//Init frontSum
for (inti = 1; i < frontSum.length; i++) {
if (frontSum[i - 1] < 0) {
frontSum[i] = nums.get(i);
} else {
frontSum[i] = frontSum[i - 1] + nums.get(i);
}
}
maxSum = frontSum[0];
//Find max
for (inti = 1; i < frontSum.length; i++) {
if (frontSum[i] < maxSum) {
frontSum[i] = maxSum;
} else {
maxSum = frontSum[i];
}
}
//Init endSum
endSum[endSum.length - 1] = nums.get(nums.size() - 1);
for (inti = endSum.length - 2; i >= 0; i--) {
if (endSum[i + 1] < 0) {
endSum[i] = nums.get(i);
} else {
endSum[i] = endSum[i + 1] + nums.get(i);
}
}
//Find max
maxSum = endSum[endSum.length - 1];
for (inti = endSum.length - 2; i >= 0; i--) {
if (endSum[i] < maxSum) {
endSum[i] = maxSum;
} else {
maxSum = endSum[i];
}
}
//Calculate max Sum
maxSum = Integer.MIN_VALUE;
for (inti = 0; i < nums.size() - 1; i++) {
maxSum = Math.max(maxSum, frontSum[i] + endSum[i + 1]);
}
returnmaxSum;
}
}
```