-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
32 lines (31 loc) · 985 Bytes
/
Solution.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
class Solution {
public int stoneGameII(int[] piles) {
Map<Integer, Integer> map = new HashMap<>();
int total = Arrays.stream(piles).sum();
return (total + dfs(0, 1, piles, map)) >> 1;
}
private int dfs(int s, int M, int[] piles, Map<Integer, Integer> map) {
if (s >= piles.length) {
return 0;
}
int key = s << 8 | M;
if (map.containsKey(key)) {
return map.get(key);
}
if (s + 2 * M >= piles.length) {
int res = 0;
for (int i = s; i < piles.length; ++i) {
res += piles[i];
}
return res;
}
int best = Integer.MIN_VALUE;
int cur = 0;
for (int x = 1; x <= 2 * M && s + x - 1 < piles.length; ++x) {
cur += piles[s + x - 1];
best = Math.max(best, cur - dfs(s + x, Math.max(x, M), piles, map));
}
map.put(key, best);
return best;
}
}