-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCan I Win.java
33 lines (30 loc) · 1.01 KB
/
Can I Win.java
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
class Solution {
// hashmap record tried states, state record used integers
Map<String, Boolean> map = new HashMap<>();
int[] state;
public boolean canIWin(int maxChoosableInteger, int desiredTotal) {
int mci = maxChoosableInteger, dt = desiredTotal;
state = new int[mci];
int max = (mci+1)*mci/2;
if(max < dt) return false;
return helper(dt);
}
public boolean helper(int dt) {
String s = Arrays.toString(state);
if(map.containsKey(s)) return map.get(s);
for(int i = 0; i < state.length; i++) {
if(state[i] == 0) {
state[i] = 1;
// if >= target or opponent lost
if(i+1 >= dt || !helper(dt-i-1)) {
map.put(s, true);
state[i] = 0;
return true;
}
state[i] = 0;
}
}
map.put(s, false);
return false;
}
}