-
Notifications
You must be signed in to change notification settings - Fork 43
/
can-i-win.py
95 lines (83 loc) · 3.09 KB
/
can-i-win.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# V0
# V1
# http://bookshadow.com/weblog/2016/11/20/leetcode-can-i-win/
class Solution(object):
def canIWin(self, maxChoosableInteger, desiredTotal):
"""
:type maxChoosableInteger: int
:type desiredTotal: int
:rtype: bool
"""
dp = dict()
def search(state, total):
for x in range(maxChoosableInteger, 0, -1):
if not state & (1 << (x - 1)):
if total + x >= desiredTotal:
dp[state] = True
return True
break
for x in range(1, maxChoosableInteger + 1):
if not state & (1 << (x - 1)):
nstate = state | (1 << (x - 1))
if nstate not in dp:
dp[nstate] = search(nstate, total + x)
if not dp[nstate]:
dp[state] = True
return True
dp[state] = False
return False
if maxChoosableInteger >= desiredTotal: return True
if (1 + maxChoosableInteger) * maxChoosableInteger < 2 * desiredTotal: return False
return search(0, 0)
# V1
# https://www.jiuzhang.com/solution/can-i-win/#tag-highlight-lang-python
# IDEA : DP
class Solution:
"""
@param maxChoosableInteger: a Integer
@param desiredTotal: a Integer
@return: if the first player to move can force a win
"""
def canIWin(self, maxChoosableInteger, desiredTotal):
if (1 + maxChoosableInteger) * maxChoosableInteger / 2 < desiredTotal:
return False
self.memo = {}
return self.helper(range(1, maxChoosableInteger + 1), desiredTotal)
def helper(self, nums, desiredTotal):
hash = str(nums)
if hash in self.memo:
return self.memo[hash]
if nums[-1] >= desiredTotal:
return True
for i in range(len(nums)):
if not self.helper(nums[:i] + nums[i+1:], desiredTotal - nums[i]):
self.memo[hash] = True
return True
self.memo[hash] = False
return False
# V2
# Time: O(n!)
# Space: O(n)
class Solution(object):
def canIWin(self, maxChoosableInteger, desiredTotal):
"""
:type maxChoosableInteger: int
:type desiredTotal: int
:rtype: bool
"""
def canIWinHelper(maxChoosableInteger, desiredTotal, visited, lookup):
if visited in lookup:
return lookup[visited]
mask = 1
for i in range(maxChoosableInteger):
if visited & mask == 0:
if i + 1 >= desiredTotal or \
not canIWinHelper(maxChoosableInteger, desiredTotal - (i + 1), visited | mask, lookup):
lookup[visited] = True
return True
mask <<= 1
lookup[visited] = False
return False
if (1 + maxChoosableInteger) * (maxChoosableInteger / 2) < desiredTotal:
return False
return canIWinHelper(maxChoosableInteger, desiredTotal, 0, {})