-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path464.cpp
More file actions
23 lines (22 loc) · 912 Bytes
/
464.cpp
File metadata and controls
23 lines (22 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
bool dp(int maxChoosableInteger, int desiredTotal, int state, vector<char>& memo) {
if (desiredTotal <= 0) return false;
if (memo[state]) return memo[state] == 1;
for (int num = 0; num < maxChoosableInteger; ++num) {
if (state & (1 << num)) continue;
if (!dp(maxChoosableInteger, desiredTotal - num - 1, state | (1 << num), memo)) {
memo[state] = 1;
return true;
}
}
memo[state] = -1;
return false;
}
bool canIWin(int maxChoosableInteger, int desiredTotal) {
if (maxChoosableInteger >= desiredTotal) return true;
if ((1 + maxChoosableInteger) * maxChoosableInteger / 2 < desiredTotal) return false;
vector<char> memo(1 << maxChoosableInteger, 0);
return dp(maxChoosableInteger, desiredTotal, 0, memo);
}
};