In the "100 game," two players take turns adding, to a running total, any integer from 1..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 of 1..15 without replacement until they reach a total >= 100.
Given an integer <code>maxChoosableInteger</code> and another integer <code>desiredTotal</code>, determine if the first player to move can force a win, assuming both players play optimally.
You can always assume that <code>maxChoosableInteger</code> will not be larger than 20 and <code>desiredTotal</code> will not be larger than 300.
Example
Approach #1: DFS. [C++]
Analysis:
State of Game: initially, we have all <code>M</code> numbers <code>[1, M]</code> available in the pool. Each number may or may not be picked at a state of the game later on, so we have maximum <code>2^M</code>different states. Note that <code>M <= 20</code>, so <code>int</code> range is enough to cover it. For memorization, we define <code>int k</code> as the key for a game state, where
the <code>i</code>-th bit of <code>k</code>, i.e., <code>k&(1<<i)</code> represents the availability of number <code>i+1</code> (<code>1</code>: picked; <code>0</code>: not picked).
At state <code>k</code>, the current player could pick any unpicked number from the pool, so state <code>k</code> can only go to one of the valid next states <code>k'</code>:
if <code>i</code>-th bit of <code>k</code> is <code>0</code>, set it to be <code>1</code>, i.e., next state <code>k' = k|(1<<i)</code>.
Reference:
https://leetcode.com/problems/can-i-win/
永遠渴望,大智若愚(stay hungry, stay foolish)