-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathstone-game-ix.py
34 lines (31 loc) · 1.25 KB
/
stone-game-ix.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
# Time: O(n)
# Space: O(1)
import collections
class Solution(object):
def stoneGameIX(self, stones):
"""
:type stones: List[int]
:rtype: bool
"""
count = collections.Counter(x%3 for x in stones)
if count[0]%2 == 0:
# iff both counts are not zero, then alice takes the least one at first, the remains are deterministic for bob to lose:
# - assumed count[1] is the least one:
# 1(,1,2)*,2,(,2)* => bob loses
# ^
# - assumed count[2] is the least one:
# 2(,2,1)*,1,(,1)* => bob loses
# ^
return count[1] and count[2]
# iff abs(count[1] - count[2]) >= 3, then alice takes the most one at first, the remains are deterministic for bob to lose:
# - assumed count[1] is the most one
# 1(,1,2)*,0,1(,2,1)*,1,(,1)* => bob loses
# ^
# 1(,1,2)*,1,0,1,(,1)* => bob loses
# ^
# - assumed count[2] is the most one
# 2(,2,1)*,0,2(,1,2)*,2,(,2)* => bob loses
# ^
# 2(,2,1)*,2,0,2,(,2)* => bob loses
# ^
return abs(count[1]-count[2]) >= 3