forked from singlemancombat/interview-preparation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanIWin.java
More file actions
38 lines (36 loc) · 1.23 KB
/
Copy pathCanIWin.java
File metadata and controls
38 lines (36 loc) · 1.23 KB
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
30
31
32
33
34
35
36
37
38
public class Solution {
public boolean canIWin(int maxChoosableInteger, int desiredTotal) {
if (desiredTotal <= maxChoosableInteger) return true;
int sum = (1 + maxChoosableInteger) * maxChoosableInteger / 2;
if (sum < desiredTotal) return false;
boolean[] used = new boolean[maxChoosableInteger + 1];
return helper(desiredTotal, used, new HashMap<Integer, Boolean>());
}
private boolean helper(int desiredTotal, boolean[] used, Map<Integer, Boolean> map) {
if (desiredTotal <= 0) return false;
int key = getKey(used);
if (map.containsKey(key))
return map.get(key);
for (int i = 1; i < used.length; i++) {
if (!used[i]) {
used[i] = true;
if (!helper(desiredTotal - i, used, map)) {
map.put(key, true);
used[i] = false;
return true;
}
used[i] = false;
}
}
map.put(key, false);
return false;
}
private int getKey(boolean[] used) {
int key = 0;
for (boolean b : used) {
key <<= 1;
key |= (b ? 1 : 0);
}
return key;
}
}