Skip to content

Commit 11a12fb

Browse files
author
Chris Wu
committed
no message
1 parent 3fd9514 commit 11a12fb

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

problems/stone-game-ii.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ def helper(start, m):
66
if start>=len(piles): return 0
77
if start+m*2>=len(piles): return sum(piles[start:])
88

9-
stones = float('-inf')
9+
max_stones = float('-inf')
1010
for x in xrange(1, m*2+1):
11-
stones = max(stones, sum(piles[start:])-helper(start+x, max(m, x)))
11+
max_stones = max(max_stones, sum(piles[start:])-helper(start+x, max(m, x)))
1212

13-
history[(start, m)] = stones
13+
history[(start, m)] = max_stones
1414
return history[(start, m)]
1515

1616
history = {}
17-
return helper(0, 1)
17+
return helper(0, 1)
18+
19+
"""
20+
helper(start, m) := the max stones that the first player will get with given piles[start:] and M.
21+
max_stones = MAX{ (sum of all the stones) - (max_stones the other player will get) } = MAX{ sum(piles[start:]) - helper(start+x, max(m, x)) }
22+
"""

0 commit comments

Comments
 (0)