-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek6 464. Can I Win
More file actions
31 lines (29 loc) · 886 Bytes
/
week6 464. Can I Win
File metadata and controls
31 lines (29 loc) · 886 Bytes
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
public class Solution {
public boolean canIWin(int maxChoosableInteger, int desiredTotal) {
if (desiredTotal <= 0){
return true;
}
if (maxChoosableInteger * (maxChoosableInteger + 1) / 2 < desiredTotal){
return false;
}
return canIWin(desiredTotal, maxChoosableInteger, 0, new HashMap<>());
}
private boolean canIWin(int total, int n, int state, HashMap<Integer,Boolean>cache)
if(total <= 0){
return false;
}
if (cache.containsKey(state)){
return cache.get(state);
}
for(int i = 0; i < n; i++){
if((state & (1 << i)) != 0){
continue;
}
if (!canIWin(total-(i+1), n, state | (1<<i), cache)){
cache.put(state, ture);
return true;
}
}
cache.put(state, false);
return false;
}