-
Notifications
You must be signed in to change notification settings - Fork 45
/
_486.java
45 lines (37 loc) · 1.06 KB
/
_486.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
/**
* LeetCode 486 - Predict the Winner
* <p>
* Game Theory + DP
*/
public class _486 {
Integer[][] f, g;
int[] nums;
// Maximize the sum
private int F(int i, int j) {
if (i == j) return nums[i];
if (f[i][j] != null) return f[i][j];
int ans = Integer.MIN_VALUE;
ans = Math.max(ans, G(i + 1, j) + nums[i]);
ans = Math.max(ans, G(i, j - 1) + nums[j]);
f[i][j] = ans;
return ans;
}
// Minimize the sum
private int G(int i, int j) {
if (i == j) return 0;
if (g[i][j] != null) return g[i][j];
int ans = Integer.MAX_VALUE;
ans = Math.min(ans, F(i + 1, j));
ans = Math.min(ans, F(i, j - 1));
g[i][j] = ans;
return ans;
}
public boolean PredictTheWinner(int[] nums) {
this.nums = nums;
f = new Integer[nums.length][nums.length];
g = new Integer[nums.length][nums.length];
int sum = 0;
for (int num : nums) sum += num;
return F(0, nums.length - 1) >= sum - F(0, nums.length - 1);
}
}