878. Picking Game
Two friends play a token game with tokens numbered 1 to maxChoosable, each token usable at most once by either player. They alternate turns, the first friend starting. On a turn a player takes one remaining token and adds its number to a shared running total. The player whose move makes the running total reach desiredTotal or more wins immediately.
Both play perfectly. Return true if the first player can force a win. If desiredTotal is 0 the first player wins at once. If all tokens together can never reach desiredTotal, nobody can win, so return false.
Example 1
- Input:
- maxChoosable = 6, desiredTotal = 11
- Output:
- true
- Explanation:
Tokens 1..6, target 11 (first player wins): pick 5; any reply leaves the first player a token that completes the 11, and the rival cannot reach 11 first.
Example 2
- Input:
- maxChoosable = 4, desiredTotal = 15
- Output:
- false
- Explanation:
The tokens 1..4 add up to only 10, so nobody can ever reach 15 and the first player cannot win.
Example 3
- Input:
- maxChoosable = 5, desiredTotal = 0
- Output:
- true
- Explanation:
A target of 0 is already met, so the first player wins immediately.
Constraints
- 1 ≤ maxChoosable ≤ 20
- 0 ≤ desiredTotal ≤ 300
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,600 msC++ 400 msJava 800 msJavaScript 800 msTypeScript 800 ms
Expected complexity
- Time
- O(2^m * m)
- Space
- O(2^m)