-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathSolution.py
68 lines (58 loc) · 2.64 KB
/
Solution.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Alice and Bob take turns playing a game, with Alice starting first.
There are n stones arranged in a row. On each player's turn, they can remove either the leftmost stone or the rightmost stone from the row and receive points equal to the sum of the remaining stones' values in the row. The winner is the one with the higher score when there are no stones left to remove.
Bob found that he will always lose this game (poor Bob, he always loses), so he decided to minimize the score's difference. Alice's goal is to maximize the difference in the score.
Given an array of integers stones where stones[i] represents the value of the ith stone from the left, return the difference in Alice and Bob's score if they both play optimally.
Example 1:
Input: stones = [5,3,1,4,2]
Output: 6
Explanation:
- Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4].
- Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4].
- Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4].
- Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4].
- Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = [].
The score difference is 18 - 12 = 6.
Example 2:
Input: stones = [7,90,5,1,100,10,10,2]
Output: 122
Constraints:
n == stones.length
2 <= n <= 1000
1 <= stones[i] <= 1000
"""
class Solution:
def stoneGameVII(self, stones: List[int]) -> int:
def take_turn(accu, start, end, track):
key = (start, end)
if key in track:
return track[key]
l = end - start + 1
if l == 1:
return 0, 0
is_alice = (len(accu) - 1 - l) % 2 == 0
s1 = accu[end] - accu[start]
s2 = accu[end + 1] - accu[start + 1]
next_a1, next_b1 = take_turn(accu, start, end - 1, track)
next_a2, next_b2 = take_turn(accu, start + 1, end, track)
if is_alice:
d1 = next_a1 + s1 - next_b1
d2 = next_a2 + s2 - next_b2
if d1 > d2:
track[key] = (next_a1 + s1, next_b1)
else:
track[key] = (next_a2 + s2, next_b2)
else:
d1 = next_a1 - s1 - next_b1
d2 = next_a2 - s2 - next_b2
if d1 > d2:
track[key] = (next_a2, next_b2 + s2)
else:
track[key] = (next_a1, next_b1 + s1)
return track[key]
track = {}
accu = [0] + list(accumulate(stones))
sa, sb = take_turn(accu, 0, len(stones) - 1, track)
return sa - sb