Can I Win Posted on 2018-10-07 Descriptionhttps://leetcode.com/problems/can-i-win/description/ Solution1234567891011121314151617181920212223242526class Solution {public: bool canIWin(int maxChoosableInteger, int desiredTotal) { if (maxChoosableInteger * (1 + maxChoosableInteger) < desiredTotal) return false; vector<char> states(1<<maxChoosableInteger, 0); helper(desiredTotal, states, 0, 1, maxChoosableInteger); return states[0] == 1; } void helper(int target, vector<char>& states, int currentState, int currentUser, int count) { if (states[currentState] != 0) return; for (int i = 0; i < count; ++i) { if (currentState & (1<<i)) continue; //Skip it when the number is taken. if ((i + 1) >= target) {states[currentState] = currentUser; return;} //Satisfy; helper(target - (i + 1), states, currentState | (1<<i), -currentUser, count); if (states[currentState | (1<<i)] == currentUser) { states[currentState] = currentUser; return; } } states[currentState] = -currentUser; return; }};