Welcome to the "Can I Win" strategy game! This is a fascinating twist on the classic number-picking game where two players compete to reach a target total, but with a crucial constraint: no number can be used twice.

The Game Rules:

  • Two players take turns picking integers from 1 to maxChoosableInteger
  • Each number can only be used once (no replacement)
  • Numbers are added to a running total
  • The first player to make the total reach or exceed desiredTotal wins
  • Both players play optimally (always make the best possible move)

Your Task: Determine if the first player can guarantee a win with optimal play. Return true if the first player has a winning strategy, false otherwise.

Example: With numbers 1-10 and target 11, the first player can pick 10, then no matter what the second player picks (1-9), the first player can reach 11+ on their next turn!

Input & Output

example_1.py โ€” Small Numbers, Easy Win
$ Input: maxChoosableInteger = 10, desiredTotal = 11
โ€บ Output: true
๐Ÿ’ก Note: The first player can pick 10, then regardless of what the second player picks (1-9), the first player can always reach 11 on their next turn. For example: First player picks 10, second player picks any number (say 3), first player picks 1 and wins with total 11.
example_2.py โ€” Impossible Target
$ Input: maxChoosableInteger = 10, desiredTotal = 40
โ€บ Output: false
๐Ÿ’ก Note: The sum of all numbers from 1 to 10 is 55. Even if the first player could use all numbers optimally, they cannot guarantee a win because the second player also plays optimally. Through game theory analysis, the second player has a winning strategy in this scenario.
example_3.py โ€” Immediate Win
$ Input: maxChoosableInteger = 20, desiredTotal = 15
โ€บ Output: true
๐Ÿ’ก Note: The first player wins immediately by picking any number >= 15. Since maxChoosableInteger (20) >= desiredTotal (15), the first player can choose 15, 16, 17, 18, 19, or 20 to win on the first move.

Visualization

Tap to expand
Can I Win: Game Strategy (maxChoosableInteger=4, desiredTotal=6)Initial StateAvailable: {1,2,3,4}Target: 6First Player TurnAfter Pick 4Available: {1,2,3}Target: 2Second Player TurnPick 4If picks 1Available: {2,3}Target: 1First wins!If picks 2Available: {1,3}Target: 0First wins!If picks 3Available: {1,2}Target: -1First wins!Strategy AnalysisFirst player's move (pick 4) forcessecond player into all losing positions๐ŸŽฏ Key Insight: The first player can guarantee a win by choosing the largest number first, limiting the opponent's options and ensuring all possible responses lead to winning positions for the first player.
Understanding the Visualization
1
Game Setup
Available numbers: {1, 2, 3, 4}, Target: 6. First player must choose wisely.
2
Optimal First Move
First player picks 4. Now second player faces {1, 2, 3} with target 2.
3
Second Player's Dilemma
Any choice by second player (1, 2, or 3) leaves first player with a winning move.
4
Guaranteed Victory
First player can always complete the remaining target on their next turn.
Key Takeaway
๐ŸŽฏ Key Insight: Use memoized game theory with bitmask representation. The optimal strategy involves trying each possible move and recursively checking if the opponent can win from the resulting state, while caching results to avoid recomputation.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(2^n * n)

There are 2^n possible game states (bitmasks), and for each state we try at most n moves, but each state is computed only once

n
2n
โš  Quadratic Growth
Space Complexity
O(2^n)

Memoization table stores results for up to 2^n different game states

n
2n
โš  Quadratic Space

Constraints

  • 1 <= maxChoosableInteger <= 20
  • 0 <= desiredTotal <= 300
  • Both players play optimally
  • Numbers from 1 to maxChoosableInteger can each be used at most once
Asked in
Google 28 Microsoft 22 Amazon 15 Meta 12
63.4K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen