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.
Input: maxChoosableInteger =10, desiredTotal =11Output: falseExplanation:
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.
We use bitmasking to represent which numbers have been picked. At each turn, the current player tries all available numbers. If picking any number leads to a state where the opponent cannot win, the current player can force a win. Memoization avoids recomputation.
classSolution {
public:bool canIWin(int n, int total) {
if ((n +1) * n /2< total) return false;
if (total ==0) return true;
std::unordered_map<int, bool> memo;
std::function<bool(int, int)> dfs = [&](int mask, int rem) {
if (memo.count(mask)) return memo[mask];
for (int i =0; i < n; ++i) {
if (!(mask & (1<< i))) {
if (i +1>= rem ||!dfs(mask | (1<< i), rem - (i +1)))
return memo[mask] = true;
}
}
return memo[mask] = false;
};
returndfs(0, total);
}
};
classSolution {
publicbooleancanIWin(int n, int total) {
if ((n + 1) * n / 2 < total) returnfalse;
if (total == 0) returntrue;
Map<Integer, Boolean> memo =new HashMap<>();
return dfs(0, total, n, memo);
}
privatebooleandfs(int mask, int rem, int n, Map<Integer, Boolean> memo) {
if (memo.containsKey(mask)) return memo.get(mask);
for (int i = 0; i < n; i++) {
if ((mask & (1 << i)) == 0) {
if (i + 1 >= rem ||!dfs(mask | (1 << i), rem - (i + 1), n, memo)) {
memo.put(mask, true);
returntrue;
}
}
}
memo.put(mask, false);
returnfalse;
}
}
classSolution {
funcanIWin(n: Int, total: Int): Boolean {
if ((n + 1) * n / 2 < total) returnfalseif (total ==0) returntrueval memo = mutableMapOf<Int, Boolean>()
fundfs(mask: Int, rem: Int): Boolean {
memo[mask]?.let { returnit }
for (i in0 until n) {
if (mask and (1 shl i) ==0) {
if (i + 1>= rem || !dfs(mask or (1 shl i), rem - (i + 1))) {
memo[mask] = truereturntrue }
}
}
memo[mask] = falsereturnfalse }
return dfs(0, total)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defcanIWin(self, n: int, total: int) -> bool:
if (n +1) * n //2< total:
returnFalseif total ==0:
returnTrue memo: dict[int, bool] = {}
defdfs(mask: int, rem: int) -> bool:
if mask in memo:
return memo[mask]
for i in range(n):
ifnot (mask & (1<< i)):
if i +1>= rem ornot dfs(mask | (1<< i), rem - (i +1)):
memo[mask] =TruereturnTrue memo[mask] =FalsereturnFalsereturn dfs(0, total)