-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19.py
More file actions
27 lines (21 loc) · 724 Bytes
/
19.py
File metadata and controls
27 lines (21 loc) · 724 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
class Solution:
def canIWin(self, n: int, total: int) -> bool:
if total <= 0:
return True
if n * (n + 1) // 2 < total:
return False
dp = {}
def helper(used, curr_sum):
if curr_sum >= total:
return False
if used in dp:
return dp[used]
for i in range(1, n + 1):
if not (used >> i) & 1:
new_used = used | (1 << i)
if curr_sum + i >= total or not helper(new_used, curr_sum + i):
dp[used] = True
return True
dp[used] = False
return False
return helper(0, 0)