Skip to content

Commit 82da6e5

Browse files
committed
Runtime: 884 ms (Top 5.06%) | Memory: 117.5 MB (Top 5.06%)
1 parent 76eff6d commit 82da6e5

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Runtime: 884 ms (Top 5.06%) | Memory: 117.5 MB (Top 5.06%)
12
class Solution {
23
public int stoneGameII(int[] piles) {
34
Map<String, Integer> memo = new HashMap<>();
@@ -7,35 +8,34 @@ public int stoneGameII(int[] piles) {
78
totalSum+=ele;
89
return (diff+totalSum)/2;
910
}
10-
11-
11+
1212
public int stoneGame(int[] piles, int M, int index, int turn,Map<String, Integer> memo )
1313
{
1414
if(index >= piles.length)
1515
return 0;
1616
if(memo.containsKey(index+"-"+M+"-"+turn))
1717
return memo.get(index+"-"+M+"-"+turn);
1818
int score=0,maxScore=Integer.MIN_VALUE;
19-
// Alice's turn
19+
// Alice's turn
2020
if(turn == 0)
2121
{
2222
for(int X=1;X<=2*M && index+X-1<piles.length;X++)
2323
{
2424
score += piles[index+X-1];
2525
maxScore= Math.max(maxScore,stoneGame(piles,Math.max(X,M),index+X,1,memo)+score);
26-
}
26+
}
2727
memo.put(index+"-"+M+"-"+turn,maxScore);
2828
return maxScore;
2929
}
3030
// Bob's turn
3131
int minScore=Integer.MAX_VALUE;
3232
for(int X=1;X<=2*M && index+X-1<piles.length;X++)
3333
{
34-
score += piles[index+X-1];
34+
score += piles[index+X-1];
3535
minScore = Math.min(minScore, stoneGame(piles,Math.max(X,M),index+X,0,memo) - score ) ;
3636
}
3737
memo.put(index+"-"+M+"-"+turn,minScore);
3838
return minScore;
3939
}
40-
41-
}
40+
41+
}

0 commit comments

Comments
 (0)