File tree Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 183 ms
2
+ class Solution {
3
+ public:
4
+ unordered_map<int , bool > Mp;
5
+ vector<bool >used;
6
+ int findkey (vector<bool >&used)
7
+ {
8
+ int num = 0 ;
9
+ for (bool a : used)
10
+ {
11
+ num <<= 1 ;
12
+ if (a) num |= 1 ;
13
+
14
+ }
15
+ return num;
16
+ }
17
+
18
+ bool solve (int dt)
19
+ {
20
+ if (dt <= 0 )
21
+ return 0 ;
22
+
23
+ int key = findkey (used);
24
+
25
+ auto it = Mp.find (key);
26
+ if (it != Mp.end ())
27
+ return it->second ;
28
+
29
+ for (int i = 1 ; i < used.size (); i++)
30
+ {
31
+ if (!used[i])
32
+ {
33
+ used[i] = 1 ;
34
+
35
+ if (!solve (dt - i))
36
+ {
37
+ Mp[key] = 1 ;
38
+ used[i] = 0 ;
39
+ return 1 ;
40
+ }
41
+ used[i] = 0 ;
42
+ }
43
+ }
44
+ Mp[key] = 0 ;
45
+ return 0 ;
46
+ }
47
+
48
+ bool canIWin (int maxChoosableInteger, int desiredTotal) {
49
+ if (desiredTotal <= 0 )
50
+ return 1 ;
51
+ int sum = (1 + maxChoosableInteger) * maxChoosableInteger / 2 ;
52
+ if (sum < desiredTotal)
53
+ return 0 ;
54
+
55
+ for (int i = 0 ; i <= maxChoosableInteger; i++)
56
+ used.push_back (0 );
57
+
58
+ return solve (desiredTotal);
59
+ }
60
+ };
You can’t perform that action at this time.
0 commit comments