Skip to content

Commit 479af3a

Browse files
authored
Create stone-game-iii.py
1 parent 0c45b68 commit 479af3a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Python/stone-game-iii.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def stoneGameIII(self, stoneValue):
6+
"""
7+
:type stoneValue: List[int]
8+
:rtype: str
9+
"""
10+
dp = [float("-inf")]*3
11+
dp[len(stoneValue)%3] = 0
12+
for i in reversed(xrange(len(stoneValue))):
13+
max_dp, curr = float("-inf"), 0
14+
for j in xrange(min(3, len(stoneValue)-i)):
15+
curr += stoneValue[i+j]
16+
max_dp = max(max_dp, curr-dp[(i+j+1)%3])
17+
dp[i%3] = max_dp
18+
return ["Tie", "Alice", "Bob"][cmp(dp[0], 0)]

0 commit comments

Comments
 (0)