|
30 | 30 | Same with other integers chosen by the first player, the second player will always win.
|
31 | 31 | */
|
32 | 32 | public class _464 {
|
33 |
| - /**Credit: https://discuss.leetcode.com/topic/68896/java-solution-using-hashmap-with-detailed-explanation*/ |
| 33 | + public static class Solution1 { |
| 34 | + /** |
| 35 | + * Credit: https://discuss.leetcode.com/topic/68896/java-solution-using-hashmap-with-detailed-explanation |
| 36 | + */ |
34 | 37 |
|
35 |
| - Map<Integer, Boolean> map; |
36 |
| - boolean[] used; |
| 38 | + Map<Integer, Boolean> map; |
| 39 | + boolean[] used; |
37 | 40 |
|
38 |
| - public boolean canIWin(int maxChoosableInteger, int desiredTotal) { |
39 |
| - int sum = (1 + maxChoosableInteger) * maxChoosableInteger / 2; |
40 |
| - if (sum < desiredTotal) { |
41 |
| - return false; |
42 |
| - } |
43 |
| - if (desiredTotal <= 0) { |
44 |
| - return true; |
45 |
| - } |
46 |
| - |
47 |
| - map = new HashMap(); |
48 |
| - used = new boolean[maxChoosableInteger + 1]; |
49 |
| - return helper(desiredTotal); |
50 |
| - } |
| 41 | + public boolean canIWin(int maxChoosableInteger, int desiredTotal) { |
| 42 | + int sum = (1 + maxChoosableInteger) * maxChoosableInteger / 2; |
| 43 | + if (sum < desiredTotal) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + if (desiredTotal <= 0) { |
| 47 | + return true; |
| 48 | + } |
51 | 49 |
|
52 |
| - public boolean helper(int desiredTotal) { |
53 |
| - if (desiredTotal <= 0) { |
54 |
| - return false; |
| 50 | + map = new HashMap(); |
| 51 | + used = new boolean[maxChoosableInteger + 1]; |
| 52 | + return helper(desiredTotal); |
55 | 53 | }
|
56 |
| - int key = format(used); |
57 |
| - if (!map.containsKey(key)) { |
58 |
| - // try every unchosen number as next step |
59 |
| - for (int i = 1; i < used.length; i++) { |
60 |
| - if (!used[i]) { |
61 |
| - used[i] = true; |
62 |
| - // check whether this lead to a win (i.e. the other player lose) |
63 |
| - if (!helper(desiredTotal - i)) { |
64 |
| - map.put(key, true); |
| 54 | + |
| 55 | + public boolean helper(int desiredTotal) { |
| 56 | + if (desiredTotal <= 0) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + int key = format(used); |
| 60 | + if (!map.containsKey(key)) { |
| 61 | + // try every unchosen number as next step |
| 62 | + for (int i = 1; i < used.length; i++) { |
| 63 | + if (!used[i]) { |
| 64 | + used[i] = true; |
| 65 | + // check whether this lead to a win (i.e. the other player lose) |
| 66 | + if (!helper(desiredTotal - i)) { |
| 67 | + map.put(key, true); |
| 68 | + used[i] = false; |
| 69 | + return true; |
| 70 | + } |
65 | 71 | used[i] = false;
|
66 |
| - return true; |
67 | 72 | }
|
68 |
| - used[i] = false; |
69 | 73 | }
|
| 74 | + map.put(key, false); |
70 | 75 | }
|
71 |
| - map.put(key, false); |
| 76 | + return map.get(key); |
72 | 77 | }
|
73 |
| - return map.get(key); |
74 |
| - } |
75 | 78 |
|
76 |
| - // transfer boolean[] to an Integer |
77 |
| - public int format(boolean[] used) { |
78 |
| - int num = 0; |
79 |
| - for (boolean b : used) { |
80 |
| - num <<= 1; |
81 |
| - if (b) { |
82 |
| - num |= 1; |
| 79 | + // transfer boolean[] to an Integer |
| 80 | + public int format(boolean[] used) { |
| 81 | + int num = 0; |
| 82 | + for (boolean b : used) { |
| 83 | + num <<= 1; |
| 84 | + if (b) { |
| 85 | + num |= 1; |
| 86 | + } |
83 | 87 | }
|
| 88 | + return num; |
84 | 89 | }
|
85 |
| - return num; |
86 | 90 | }
|
87 |
| - |
88 | 91 | }
|
0 commit comments