Can I Win Problem
Description
LeetCode Problem 464.
In the "100 game" two players take turns adding, to a running total, any integer from 1 to 10. The player who first causes the running total to reach or exceed 100 wins.
What if we change the game so that players cannot re-use integers?
For example, two players might take turns drawing from a common pool of numbers from 1 to 15 without replacement until they reach a total >= 100.
Given two integers maxChoosableInteger and desiredTotal, return true if the first player to move can force a win, otherwise, return false. Assume both players play optimally.
Example 1:
Input: maxChoosableInteger = 10, desiredTotal = 11
Output: false
Explanation:
No matter which integer the first player choose, the first player will lose.
The first player can choose an integer from 1 up to 10.
If the first player choose 1, the second player can only choose integers from 2 up to 10.
The second player will win by choosing 10 and get a total = 11, which is >= desiredTotal.
Same with other integers chosen by the first player, the second player will always win.
Example 2:
Input: maxChoosableInteger = 10, desiredTotal = 0
Output: true
Example 3:
Input: maxChoosableInteger = 10, desiredTotal = 1
Output: true
Constraints:
- 1 <= maxChoosableInteger <= 20
- 0 <= desiredTotal <= 300
Solution
Approach: Game Search With a Memo on the Used Numbers
A player to move can force a win exactly when some available number either reaches the total right away or leaves the opponent in a position the opponent cannot win. That is a recursive definition, and searching every order of moves would take up to 20! steps. But many orders lead to the same position.
The key observation: a position depends only on which numbers have been used, not on the order they were taken in. The used numbers also fix the running total. So the state fits in a bit mask used with bit i - 1 set when i is taken, and there are at most 220 (about a million) states. Solve each one once and store the result in memo[used].
canWin(used, remaining), for the player to move:
- If
memo[used]is known, return it. - For each number not in
used: ifnumber >= remaining, this move wins. Otherwise, ifcanWin(used | bit, remaining - number)is false, the opponent loses after this move, so it wins too. - If no number works, the player to move loses. Store the result before returning.
The detail people miss: check first whether 1 + 2 + ... + maxChoosableInteger is at least desiredTotal. If it is not, nobody can ever reach the total and the answer is false, but the recursion alone would treat "no numbers left" as a loss for whoever moves last and could wrongly report a win for the first player. A desiredTotal of 0 needs no special case: the first number already reaches it.
Complexity: O(2m · m) time and O(2m) space, where m is maxChoosableInteger.
class Solution:
def canIWin(self, maxChoosableInteger: int, desiredTotal: int) -> bool:
# If all numbers together cannot reach the total, nobody ever wins.
if maxChoosableInteger * (maxChoosableInteger + 1) // 2 < desiredTotal:
return False
# memo[used] for a bit mask of the numbers already taken: 0 = not computed yet,
# 1 = the player to move wins, -1 = the player to move loses.
# The mask alone fixes the running total, so it identifies the whole game state.
memo = [0] * (1 << maxChoosableInteger)
def can_win(used: int, remaining: int) -> bool:
if memo[used] != 0:
return memo[used] == 1
for number in range(1, maxChoosableInteger + 1):
bit = 1 << (number - 1)
if used & bit:
continue
# Win now by reaching the total, or move to a state the opponent loses.
if number >= remaining or not can_win(used | bit, remaining - number):
memo[used] = 1
return True
memo[used] = -1
return False
return can_win(0, desiredTotal)
Now write it from memory
Close the write-up and solve it yourself against the tests.