-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-1049-Last-Stone-Weight-II.java
438 lines (341 loc) · 14.6 KB
/
LeetCode-1049-Last-Stone-Weight-II.java
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
Similar to: https://leetcode.com/problems/target-sum/
这题实际上就是在每个element前面加 +,-,然后求和。如果最后的和是负,那我们可以等价转换成整数,比如:
[1,2,3]
[-1,2,-3], sum -2, could be convert to:
[1,-2,3], sum +2
所以我们就减少了一半的运算。
*/
class Solution {
// 1.DFS without optimization (TLE)
/*
Adding + or - to each number, and get the value to be minimum.
*/
// int min = Integer.MAX_VALUE;
// public int lastStoneWeightII(int[] stones) {
// int n = stones.length;
// /*
// 1 <= stones.length <= 30
// 1 <= stones[i] <= 100
// sum -3000 - 0 - 3000
// index 0 - 3000 - 6000
// subproblem:
// mem[i][j] - means number of sum answers, using the nums [0, i] and add +,- to the element, and get the number of different sum
// */
// DFS(stones, 0, 0);
// return min;
// }
// private void DFS(int[] stones, int idx, int sum) {
// if (idx == stones.length) {
// if (sum >= 0) {
// min = Math.min(min, sum);
// }
// return;
// }
// DFS(stones, idx + 1, sum + stones[idx]);
// DFS(stones, idx + 1, sum - stones[idx]);
// }
// 2.DFS with optimization (using memorized array)
/*
https://leetcode.com/problems/last-stone-weight-ii/discuss/313051/Simple-Java-DFS%2BMemoization-Solution-Knapsacktarget-sum
*/
// public int lastStoneWeightII(int[] stones) {
// int n = stones.length;
// /*
// 1 <= stones.length <= 30
// 1 <= stones[i] <= 100
// sum -3000 - 0 - 3000
// index 0 - 3000 - 6000
// subproblem:
// mem[i][j] - means the minimum lost weight of the nums [0, i] and add +,- to the elements
// */
// Integer[][] mem = new Integer[n + 1][6001];
// return DFS(stones, 0, 0, mem);
// }
// private int DFS(int[] stones, int idx, int sum, Integer[][] mem) {
// if (idx == stones.length) {
// /*
// the weight left must be negative
// [1,2,3]
// [-1,2,-3], sum -2, could be convert to:
// [1,-2,3], sum +2
// */
// return Math.abs(sum);
// }
// int t = sum + 3000; // making the sum + offset to get the real index
// if (mem[idx][t] != null) return mem[idx][t]; // optimization
// mem[idx][t] = Math.min(DFS(stones, idx + 1, sum + stones[idx], mem), DFS(stones, idx + 1, sum - stones[idx], mem));
// return mem[idx][t];
// }
// 3.DP (pull, my own solution)
/*
subproblem:
dp[i][j] - minimum weight by calculating the sum of nums [0, i], with adding +,- in front of each element
recurrence relation:
for (int i = 1; i <= n; i++) {
for (k = 0; k <= sum; k++) {
if (dp[i - 1][k] != Integer.MAX_VALUE) {
int sumPlus = Math.abs(dp[i - 1][k] + stones[k - 1]);
dp[i][sumPlus] = Math.min(dp[i][sumPlus], sumPlus);
int sumMinus = Math.abs(dp[i - 1][k] - stones[k - 1]);
dp[i][sumMinus] = Math.min(dp[i][sumMinus], sumMinus);
}
}
}
init
d[0][0-sum] = Integer.MAX_VALUE
ans
min positive (d[[0][0-sum]])
Time Complexity O(N*sum) - N is number of elements in array, sum is sum of all elements
Space: O(N*sum)
Runtime: 2 ms, faster than 73.85% of Java online submissions for Last Stone Weight II.
Memory Usage: 35.8 MB, less than 100.00% of Java online submissions for Last Stone Weight II.
*/
// public int lastStoneWeightII(int[] stones) {
// int n = stones.length;
// int sum = 0;
// for (int i : stones) sum += i;
// // subproblem: dp[i][j] means the minimum lost weight by calculating the sum of nums [0, i], with +,- in front of each element
// int[][] dp = new int[n + 1][sum + 1];
// // init
// for (int i = 1; i <= n; i++) {
// for (int j = 0; j <= sum; j++) {
// dp[i][j] = Integer.MAX_VALUE;
// }
// }
// dp[1][stones[0]] = stones[0];
// // recurrence relation
// for (int i = 2; i <= n; i++) {
// for (int j = 0; j <= sum; j++) {
// if (dp[i - 1][j] != Integer.MAX_VALUE) {
// int sumPlus = Math.abs(dp[i - 1][j] + stones[i - 1]);
// dp[i][sumPlus] = Math.min(dp[i][sumPlus], sumPlus);
// int sumMinus = Math.abs(dp[i - 1][j] - stones[i - 1]);
// dp[i][sumMinus] = Math.min(dp[i][sumMinus], sumMinus);
// }
// }
// }
// int min = Integer.MAX_VALUE;
// for (int j = 0; j <= sum; j++) {
// min = Math.min(min, dp[n][j]);
// }
// return min;
// }
// 3.DP (push, my own solution)
/*
Runtime: 2 ms, faster than 73.85% of Java online submissions for Last Stone Weight II.
Memory Usage: 35.7 MB, less than 100.00% of Java online submissions for Last Stone Weight II.
*/
// public int lastStoneWeightII(int[] stones) {
// int n = stones.length;
// int sum = 0;
// for (int i : stones) sum += i;
// // subproblem: dp[i][j] means the minimum lost weight by calculating the sum of stones [0, i], with +,- in front of each element
// int[][] dp = new int[n + 1][sum + 1];
// // initialize
// for (int i = 0; i <= n; i++) {
// for (int j = 0; j <= sum; j++) {
// dp[i][j] = Integer.MAX_VALUE;
// }
// }
// dp[1][stones[0]] = stones[0];
// // recurrence relation
// for (int i = 1; i <= n - 1; i++) {
// for (int j = 0; j <= sum; j++) {
// if (dp[i][j] != Integer.MAX_VALUE) {
// int sumPlus = Math.abs(dp[i][j] + stones[i]);
// dp[i + 1][sumPlus] = Math.min(dp[i + 1][sumPlus], sumPlus);
// int sumMinus = Math.abs(dp[i][j] - stones[i]);
// dp[i + 1][sumMinus] = Math.min(dp[i + 1][sumMinus], sumMinus);
// }
// }
// }
// int min = Integer.MAX_VALUE;
// for (int j = 0; j <= sum; j++) {
// min = Math.min(min, dp[n][j]);
// }
// return min;
// }
// DP (pull && Optimized using Boolean)
// public int lastStoneWeightII(int[] stones) {
// int n = stones.length;
// int sum = 0;
// for (int i : stones) sum += i;
// // subproblem: dp[i][j] means using the elements stones [0, i], with adding +,- in front of each element, we could sum up to j (true if we could, false if we could not)
// boolean[][] dp = new boolean[n + 1][sum + 1];
// // init
// dp[0][0] = true;
// // recurrence relation
// for (int i = 1; i <= n; i++) {
// for (int j = 0; j <= sum; j++) {
// if (dp[i - 1][j]) {
// int sumPlus = Math.abs(j + stones[i - 1]);
// dp[i][sumPlus] |= dp[i - 1][j];
// int sumMinus = Math.abs(j - stones[i - 1]);
// dp[i][sumMinus] |= dp[i - 1][j];
// }
// }
// }
// // ans
// for (int j = 0; j <= sum; j++) {
// if (dp[n][j]) return j;
// }
// return -1;
// }
// // DP: Pull & Optimized using boolean & Using one dimension array
// public int lastStoneWeightII(int[] stones) {
// int n = stones.length;
// int sum = 0;
// for (int i : stones) sum += i;
// // subproblem: dp[j] means using the elements stones [0, i], with adding +,- in front of each element, we could sum up to j (true if we could, false if we could not)
// boolean[] dp = new boolean[sum + 1];
// // init
// dp[0] = true;
// // recurrence relation
// for (int i = 1; i <= n; i++) {
// boolean[] temp = new boolean[sum + 1];
// for (int j = 0; j <= sum; j++) {
// if (dp[j]) {
// int sumPlus = Math.abs(j + stones[i - 1]);
// temp[sumPlus] |= dp[j];
// int sumMinus = Math.abs(j - stones[i - 1]);
// temp[sumMinus] |= dp[j];
// }
// }
// dp = temp;
// }
// // ans
// for (int j = 0; j <= sum; j++) {
// if (dp[j]) return j;
// }
// return -1;
// }
// // Push (Optimized using boolean)
// public int lastStoneWeightII(int[] stones) {
// int n = stones.length;
// int sum = 0;
// for (int i : stones) sum += i;
// // subproblem: dp[i][j] means using the elements stones [0, i], with adding +,- in front of each element, we could sum up to j (true if we could, false if we could not)
// boolean[][] dp = new boolean[n + 1][sum + 1];
// // init
// dp[0][0] = true;
// // recurrence relation
// for (int i = 0; i < n; i++) {
// for (int j = 0; j <= sum; j++) {
// if (dp[i][j]) {
// int sumPlus = Math.abs(j + stones[i]);
// dp[i + 1][sumPlus] = true;
// int sumMinus = Math.abs(j - stones[i]);
// dp[i + 1][sumMinus] = true;
// }
// }
// }
// // ans
// for (int j = 0; j <= sum; j++) {
// if (dp[n][j]) return j;
// }
// return -1;
// }
//Push (Optimized using boolean && using 1-d array)
// public int lastStoneWeightII(int[] stones) {
// int n = stones.length;
// int sum = 0;
// for (int i : stones) sum += i;
// // subproblem: dp[j] means using the elements stones [0, i], with adding +,- in front of each element, we could sum up to j (true if we could, false if we could not)
// boolean[] dp = new boolean[sum + 1];
// // init
// dp[0] = true;
// // recurrence relation
// for (int i = 0; i < n; i++) {
// boolean[] temp = new boolean[sum + 1];
// for (int j = 0; j <= sum; j++) {
// if (dp[j]) {
// int sumPlus = Math.abs(j + stones[i]);
// temp[sumPlus] = true;
// int sumMinus = Math.abs(j - stones[i]);
// temp[sumMinus] = true;
// }
// }
// dp = temp;
// }
// // ans
// for (int j = 0; j <= sum; j++) {
// if (dp[j]) return j;
// }
// return -1;
// }
// 4. DP (Convert to subset problem)
/*
Use P to denote a subset, all elements add + in front of the elements
Use N to denote a subset, all elements add - in front of the elements
sum(P) - sum(N) = target (target is the minimum abs of positive or negative value, as a mentioned before negative sum could be equally convert to positive sum)
sum(P) - sum(N) + sum(P) + sum(N) = target + sum(P) + sum(N)
2 * sum(P) = target + sum (target + sum must be an even number)
so target = 2 * sum(P) - sum, making target to be minimuze abs positive or negative number
We convert the problem to be find a subset, the sum is equal to (target + sum) / 2 to be minimum positive number. And the target is the returned value.
subproblem:
dp[i][j] - from the subset in nums [0,i], with adding +,- in front of each element, calculate the sum, which is j
recurrence relation:
dp[i][j] = dp[i - 1][j]
dp[i][j] |= dp[i - 1][j - nums[i - 1]] if j - nums[i - 1] >= 0
init:
dp[0][0] = true
ans:
find the j that makes |2*j - sum| minimum, and return the minimum result
*/
// public int lastStoneWeightII(int[] stones) {
// int n = stones.length;
// int sum = 0;
// for (int i : stones) sum += i;
// // subproblem: the sum of subset stones [0,j] with adding +,-, in front of each element, calculated sum is j
// boolean[][] dp = new boolean[n + 1][sum + 1];
// // init
// dp[0][0] = true;
// // recurrence relation
// for (int i = 1; i <= n; i++) {
// for (int j = 0; j <= sum; j++) {
// dp[i][j] = dp[i - 1][j];
// if (j - stones[i - 1] >= 0) {
// dp[i][j] |= dp[i - 1][j - stones[i - 1]];
// }
// }
// }
// // ans
// int minVal = Integer.MAX_VALUE;
// for (int j = 0; j <= sum; j++) {
// if (dp[n][j] && minVal > Math.abs(2 * j - sum)) {
// minVal = Math.abs(2 * j - sum);
// }
// }
// return minVal;
// }
// DP (Subset && Optimized to use 1D array)
public int lastStoneWeightII(int[] stones) {
int n = stones.length;
int sum = 0;
for (int i : stones) sum += i;
// subproblem: the sum of subset stones [0,j] with adding +,-, in front of each element, calculated sum is j
boolean[] dp = new boolean[sum + 1];
// init
dp[0] = true;
// recurrence relation
for (int i = 1; i <= n; i++) {
boolean[] temp = new boolean[sum + 1];
for (int j = 0; j <= sum; j++) {
temp[j] = dp[j];
if (j - stones[i - 1] >= 0) {
temp[j] |= dp[j - stones[i - 1]];
}
}
dp = temp;
}
// ans
int minVal = Integer.MAX_VALUE;
for (int j = 0; j <= sum; j++) {
if (dp[j] && minVal > Math.abs(2 * j - sum)) {
minVal = Math.abs(2 * j - sum);
}
}
return minVal;
}
}