1
+ // Runtime: 884 ms (Top 5.06%) | Memory: 117.5 MB (Top 5.06%)
1
2
class Solution {
2
3
public int stoneGameII (int [] piles ) {
3
4
Map <String , Integer > memo = new HashMap <>();
@@ -7,35 +8,34 @@ public int stoneGameII(int[] piles) {
7
8
totalSum +=ele ;
8
9
return (diff +totalSum )/2 ;
9
10
}
10
-
11
-
11
+
12
12
public int stoneGame (int [] piles , int M , int index , int turn ,Map <String , Integer > memo )
13
13
{
14
14
if (index >= piles .length )
15
15
return 0 ;
16
16
if (memo .containsKey (index +"-" +M +"-" +turn ))
17
17
return memo .get (index +"-" +M +"-" +turn );
18
18
int score =0 ,maxScore =Integer .MIN_VALUE ;
19
- // Alice's turn
19
+ // Alice's turn
20
20
if (turn == 0 )
21
21
{
22
22
for (int X =1 ;X <=2 *M && index +X -1 <piles .length ;X ++)
23
23
{
24
24
score += piles [index +X -1 ];
25
25
maxScore = Math .max (maxScore ,stoneGame (piles ,Math .max (X ,M ),index +X ,1 ,memo )+score );
26
- }
26
+ }
27
27
memo .put (index +"-" +M +"-" +turn ,maxScore );
28
28
return maxScore ;
29
29
}
30
30
// Bob's turn
31
31
int minScore =Integer .MAX_VALUE ;
32
32
for (int X =1 ;X <=2 *M && index +X -1 <piles .length ;X ++)
33
33
{
34
- score += piles [index +X -1 ];
34
+ score += piles [index +X -1 ];
35
35
minScore = Math .min (minScore , stoneGame (piles ,Math .max (X ,M ),index +X ,0 ,memo ) - score ) ;
36
36
}
37
37
memo .put (index +"-" +M +"-" +turn ,minScore );
38
38
return minScore ;
39
39
}
40
-
41
- }
40
+
41
+ }
0 commit comments