-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathstone-game.py
More file actions
34 lines (26 loc) · 854 Bytes
/
stone-game.py
File metadata and controls
34 lines (26 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from functools import lru_cache
class Solution:
def stoneGame(self, piles):
@lru_cache(None)
def dp(left, right):
if left > right:
return 0
first = (right - left + len(piles)) % 2 == 1
if first:
return max(
dp(left + 1, right) + piles[left],
dp(left, right - 1) + piles[right]
)
else:
return min(
dp(left + 1, right) - piles[left],
dp(left, right - 1) - piles[right]
)
return dp(0, len(piles) - 1) > 0
def stoneGameMathematical(self, piles):
return True
class TestSolution:
def setup(self):
self.sol = Solution()
def test_custom1(self):
assert self.sol.stoneGame([1,2,3,4])