/
StoneGame.java
39 lines (32 loc) · 981 Bytes
/
StoneGame.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
// https://leetcode.com/problems/stone-game/
class Solution {
public boolean stoneGame(int[] piles) {
int[] points = new int[2];
int[][][] dp = new int[2][piles.length][piles.length];
return check(piles, 0, piles.length - 1, points, 0, dp);
}
boolean check(
int[] piles,
int l,
int r,
int[] points,
int player,
int[][][] dp
) {
if (l > r) {
return points[0] > points[1];
}
if (dp[player % 2][l][r] != 0) {
return dp[player % 2][l][r] == 1;
}
boolean res = false;
points[player % 2] += piles[l];
res |= check(piles, l + 1, r, points, player + 1, dp);
points[player % 2] -= piles[l];
points[player % 2] += piles[r];
res |= check(piles, l, r - 1, points, player + 1, dp);
points[player % 2] -= piles[r];
dp[player % 2][l][r] = res ? 1 : 2;
return res;
}
}