Skip to content

Commit 4b84d48

Browse files
refactor 464
1 parent 557e8fd commit 4b84d48

File tree

1 file changed

+45
-42
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+45
-42
lines changed

src/main/java/com/fishercoder/solutions/_464.java

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -30,59 +30,62 @@
3030
Same with other integers chosen by the first player, the second player will always win.
3131
*/
3232
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+
*/
3437

35-
Map<Integer, Boolean> map;
36-
boolean[] used;
38+
Map<Integer, Boolean> map;
39+
boolean[] used;
3740

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+
}
5149

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);
5553
}
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+
}
6571
used[i] = false;
66-
return true;
6772
}
68-
used[i] = false;
6973
}
74+
map.put(key, false);
7075
}
71-
map.put(key, false);
76+
return map.get(key);
7277
}
73-
return map.get(key);
74-
}
7578

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+
}
8387
}
88+
return num;
8489
}
85-
return num;
8690
}
87-
8891
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy