-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstoneGameII.java
More file actions
26 lines (23 loc) · 787 Bytes
/
stoneGameII.java
File metadata and controls
26 lines (23 loc) · 787 Bytes
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
class Solution {
public int stoneGameII(int[] piles) {
int n = piles.length;
int[][] dp = new int[n][n + 1];
int[] suffixSum = new int[n];
suffixSum[n - 1] = piles[n - 1];
for (int i = n - 2; i >= 0; i--) {
suffixSum[i] = suffixSum[i + 1] + piles[i];
}
for (int i = n - 1; i >= 0; i--) {
for (int M = 1; M <= n; M++) {
if (i + 2 * M >= n) {
dp[i][M] = suffixSum[i];
} else {
for (int X = 1; X <= 2 * M; X++) {
dp[i][M] = Math.max(dp[i][M], suffixSum[i] - dp[i + X][Math.max(M, X)]);
}
}
}
}
return dp[0][1];
}
}