-
Notifications
You must be signed in to change notification settings - Fork 359
/
s1.cpp
27 lines (26 loc) · 800 Bytes
/
s1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
private:
map<unsigned, bool> m;
unsigned used = 0;
int maxInt;
bool rec(int total) {
if (m.find(used) != m.end()) return m[used];
bool canWin = false;
for (int i = 1; i <= maxInt && !canWin; ++i) {
unsigned mask = (1 << i);
if (!(used & mask)) {
used |= mask;
canWin = i >= total || !rec(total - i);
used &= ~mask;
}
}
return m[used] = canWin;
}
public:
bool canIWin(int maxChoosableInteger, int desiredTotal) {
if (desiredTotal <= 0) return true;
if (desiredTotal > (1 + maxChoosableInteger) * maxChoosableInteger / 2) return false;
maxInt = maxChoosableInteger;
return rec(desiredTotal);
}
};