Skip to content

Commit c3a1bcc

Browse files
authored
Create stone-game-ii.py
1 parent 6c08280 commit c3a1bcc

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Python/stone-game-ii.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(n*(logn)^2)
2+
# Space: O(nlogn)
3+
4+
class Solution(object):
5+
def stoneGameII(self, piles):
6+
"""
7+
:type piles: List[int]
8+
:rtype: int
9+
"""
10+
def dp(piles, lookup, i, m):
11+
if i+2*m >= len(piles):
12+
return piles[i]
13+
if (i, m) not in lookup:
14+
lookup[i, m] = piles[i] - \
15+
min(dp(piles, lookup, i+x, max(m, x))
16+
for x in xrange(1, 2*m+1))
17+
return lookup[i, m]
18+
19+
for i in reversed(xrange(len(piles)-1)):
20+
piles[i] += piles[i+1]
21+
return dp(piles, {}, 0, 1)

0 commit comments

Comments
 (0)