Watch 10 video solutions for Can I Win, a medium level problem involving Math, Dynamic Programming, Bit Manipulation. This walkthrough by Hua Hua has 10,434 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 <= 200 <= desiredTotal <= 300Problem Overview: Two players take turns picking numbers from 1..maxChoosableInteger without reuse. Each picked number adds to a running total. The player who first makes the total reach or exceed desiredTotal wins. The task is to determine if the first player can force a win assuming both players play optimally.
Approach 1: Dynamic Programming with Bitmask (O(n · 2^n) time, O(2^n) space)
Represent the game state using a bitmask where each bit indicates whether a number has already been chosen. A mask of size n uniquely defines which moves remain. From any state, iterate through numbers 1..n; if number i is unused, simulate picking it and check whether the opponent loses from the resulting state. Memoize results for each mask so the same state is never recomputed. The key insight: if any move forces the opponent into a losing state, the current state is winning. This transforms the exponential search tree into a manageable 2^n state DP using Bit Manipulation and Dynamic Programming.
Approach 2: Minimax with Memoization (O(n · 2^n) time, O(2^n) space)
This models the problem as a classic two-player optimal strategy game. Use a recursive minimax search where the current player tries to maximize their chance of winning while the opponent tries to minimize it. At each step, iterate through available numbers and check if choosing one either immediately reaches the target or leads to a state where the opponent cannot win. Cache results using memoization keyed by the bitmask of chosen numbers. Without caching the recursion would explore up to n! paths, but memoization collapses equivalent states to 2^n. This approach highlights the Game Theory reasoning behind the optimal play.
Approach 3: Recursive DP with State Pruning (O(n · 2^n) time, O(2^n) space)
Another formulation stores the remaining total instead of the running sum and recursively tries available numbers. Before recursion, perform quick pruning checks. If the sum of all numbers 1..n is smaller than desiredTotal, winning is impossible. If any single number already reaches the target, the first player wins immediately. During recursion, subtract the chosen number from the remaining total and continue exploring unused numbers using a memo table indexed by the bitmask state. This is essentially top‑down DP with memoization but framed around remaining score rather than accumulated score.
Recommended for interviews: Use the minimax or bitmask DP approach with memoization. Interviewers expect you to recognize the game-state search and compress it using a bitmask. A naive recursive search demonstrates understanding of the game tree, but adding memoization and representing states with bit operations shows strong problem‑solving skill and reduces the complexity to O(n · 2^n).
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming with Bitmask | O(n · 2^n) | O(2^n) | Best general solution when n ≤ 20 and states can be represented as used-number masks |
| Minimax with Memoization | O(n · 2^n) | O(2^n) | When reasoning about optimal play in two-player games |
| Recursive DP with State Pruning | O(n · 2^n) | O(2^n) | Useful when framing the state as remaining target and applying early pruning checks |