Can I Win LeetCode solution in Python

1
class Solution:
2
def canIWin(self, maxChoosableInteger: int, desiredTotal: int) -> bool:
3
remainder = [i + 1 for i in range(maxChoosableInteger)] # numbers
4

5
@cache
6
def can_win(total, remainder):
7
if total >= desiredTotal:
8
return False # total is already exceed the desiredTotal. Opponent won.
9

10
for num in remainder:
11
if (
12
can_win(total + num, tuple([n for n in remainder if n != num]))
13
== False
14
): # if opponent lose, I win(return True)
15
return True
16
return False
17

18
if desiredTotal == 0:
19
return True
20
if sum(remainder) < desiredTotal: # Both of two cannot win.
21
return False
22
return can_win(0, tuple(remainder))

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0