- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlipGameII.java
More file actions
Latest commit
50 lines (37 loc) · 1.19 KB
/
Copy pathFlipGameII.java
File metadata and controls
50 lines (37 loc) · 1.19 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
39
40
41
42
43
44
45
46
47
48
49
50
importjava.util.HashMap;
importjava.util.Map;
publicclassFlipGameII {
/**
AmazonDebug 1:
Recursion
T(N) = (N - 1) * T(N - 2) = (N - 1) * (N - 3) * T(N - 4) = (N - 1) !
canWin("++++++") = !canWin("--++++") || !canWin("+--+++") || !canWin("++--++") || !canWin("+++--+") || !canWin("++++--")
AmazonDebug 2:
Recursion + memoriztion
*/
publicbooleancanWin(Strings) {
returncanWinHelper(s.toCharArray(), newHashMap<>());
}
publicbooleancanWinHelper(char[] chars, Map<char[], Boolean> map) {
if(map.get(chars) != null) {
returnmap.get(chars);
}
booleanres = false;
for(inti = 0; i < chars.length - 1; i++) {
if(chars[i] == '+' && chars[i + 1] =='+') {
chars[i] = '-';
chars[i + 1] = '-';
res = res || !canWinHelper(chars, map);
chars[i] = '+';
chars[i + 1] = '+';
}
}
map.put(chars.clone(), res);
returnres;
}
publicstaticvoidmain(String[] args) {
Stringinput = "++++";
FlipGameIIa = newFlipGameII();
a.canWin(input);
}
}