Opening the reading…
Opening the reading…
DYNAMIC PROGRAMMING LEVEL 2 › DP WITH BITMASK
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →At any turn, the history matters only through the set of numbers that have already been used. The order of earlier picks does not change which moves remain, and the cumulative sum can be recovered from that set. Represent the used numbers with one bit per integer, so one mask completely describes a game state.
Define canWin(mask) as whether the player whose turn it is can force a win from that mask. That player tries every unused number. Choosing a number wins immediately if the new sum reaches desiredTotal; otherwise, the move is winning when it leaves the opponent in a state where canWin is false. Since the same mask can be reached through different move orders, memoization avoids solving it repeatedly.
There is also a necessary impossibility check before the search. If all numbers together sum to less than desiredTotal, even taking every number cannot reach the target, so nobody can win and the first player cannot force a win. A target of zero is already reached before the first move, so the first player wins immediately.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n x 2^n) | There are at most 2^n masks, and each computed mask scans n numbers to recover its sum and scans up to n numbers to test moves. Memoization ensures each mask is expanded once, so no state is recomputed through another sequence of picks. |
| Space | O(2^n) extra | The memo array stores one result per mask. The recursion stack can add O(n), which is smaller than O(2^n) for the given range. The returned boolean is not stored as output data, and no output array is counted. |
#include <vector>
using namespace std;
class Solution {
int maxVal;
int target;
vector<int> memo;
bool canWin(int mask) {
if (memo[mask] != -1) {
return memo[mask];
}
int currentSum = 0;
for (int value = 1; value <= maxVal; ++value) {
if (mask & (1 << (value - 1))) {
currentSum += value;
}
}
for (int value = 1; value <= maxVal; ++value) {
int bit = 1 << (value - 1);
if ((mask & bit) == 0) {
if (currentSum + value >= target) {
return memo[mask] = true;
}
if (!canWin(mask | bit)) {
return memo[mask] = true;
}
}
}
return memo[mask] = false;
}
public:
bool canIWin(int maxChoosableInteger, int desiredTotal) {
int total = maxChoosableInteger * (maxChoosableInteger + 1) / 2;
if (total < desiredTotal) {
return false;
}
if (desiredTotal <= 0) {
return true;
}
maxVal = maxChoosableInteger;
target = desiredTotal;
memo.assign(1 << maxVal, -1);
return canWin(0);
}
};The expression 1 << (value - 1) assigns value 1 to the lowest bit, value 2 to the next bit, and so on. Marking a choice uses mask | bit; checking whether a choice is unused uses mask & bit. The initial mask is zero because no number has been selected before the first player's turn.