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 28 29
| class Solution { public: bool canIWin(int stable, int desired) { if (stable >= desired) return true;
if (stable * (stable + 1) / 2 < desired) return false;
unordered_map<int, bool> up; return canWin(stable, desired, 0, up); }
bool dfs(int n, int sum, int k, unordered_map<int, bool>& up) { if (up.count(k)) return up[k];
for (int i = 0; i < n; ++i) { int cur = (1 << i); if ((cur & k) == 0) { if (sum <= i + 1 || !dfs(n, sum - (i + 1), cur | k, up)) { up[k] = true; return true; } } } up[k] = false; return false; } };
|