Skip to content

Commit dd08bf7

Browse files
refactor 488
1 parent 8bb85bb commit dd08bf7

File tree

1 file changed

+44
-39
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+44
-39
lines changed

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

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,54 +38,59 @@ Each time, you may choose a ball in your hand, and insert it into the row (inclu
3838
*/
3939
public class _488 {
4040

41-
/**credit: https://discuss.leetcode.com/topic/79820/short-java-solution-beats-98
42-
* Two layer of recursion, pretty cool!*/
41+
public static class Solution1 {
4342

44-
int maxcount = 6; // the max balls you need will not exceed 6 since "The number of balls in your hand won't exceed 5"
43+
/**
44+
* credit: https://discuss.leetcode.com/topic/79820/short-java-solution-beats-98
45+
* Two layer of recursion, pretty cool!
46+
*/
4547

46-
public int findMinStep(String board, String hand) {
47-
int[] handCount = new int[26];
48-
for (int i = 0; i < hand.length(); ++i) {
49-
++handCount[hand.charAt(i) - 'A'];
50-
}
51-
int result = dfs(board + "#", handCount); // append a "#" to avoid special process while j==board.length, make the code shorter.
52-
return result == maxcount ? -1 : result;
53-
}
48+
int maxcount = 6; // the max balls you need will not exceed 6 since "The number of balls in your hand won't exceed 5"
5449

55-
private int dfs(String s, int[] handCount) {
56-
s = removeConsecutive(s);
57-
if (s.equals("#")) {
58-
return 0;
59-
}
60-
int result = maxcount;
61-
int need = 0;
62-
for (int i = 0, j = 0 ; j < s.length(); ++j) {
63-
if (s.charAt(j) == s.charAt(i)) {
64-
continue;
65-
}
66-
need = 3 - (j - i); //balls need to remove current consecutive balls.
67-
if (handCount[s.charAt(i) - 'A'] >= need) {
68-
handCount[s.charAt(i) - 'A'] -= need;
69-
result = Math.min(result, need + dfs(s.substring(0, i) + s.substring(j), handCount));
70-
handCount[s.charAt(i) - 'A'] += need;
50+
public int findMinStep(String board, String hand) {
51+
int[] handCount = new int[26];
52+
for (int i = 0; i < hand.length(); ++i) {
53+
++handCount[hand.charAt(i) - 'A'];
7154
}
72-
i = j;
55+
int result = dfs(board + "#", handCount); // append a "#" to avoid special process while j==board.length, make the code shorter.
56+
return result == maxcount ? -1 : result;
7357
}
74-
return result;
75-
}
7658

77-
//remove consecutive balls longer than 3
78-
private String removeConsecutive(String board) {
79-
for (int i = 0, j = 0; j < board.length(); ++j) {
80-
if (board.charAt(j) == board.charAt(i)) {
81-
continue;
59+
private int dfs(String s, int[] handCount) {
60+
s = removeConsecutive(s);
61+
if (s.equals("#")) {
62+
return 0;
8263
}
83-
if (j - i >= 3) {
84-
return removeConsecutive(board.substring(0, i) + board.substring(j));
85-
} else {
64+
int result = maxcount;
65+
int need = 0;
66+
for (int i = 0, j = 0; j < s.length(); ++j) {
67+
if (s.charAt(j) == s.charAt(i)) {
68+
continue;
69+
}
70+
need = 3 - (j - i); //balls need to remove current consecutive balls.
71+
if (handCount[s.charAt(i) - 'A'] >= need) {
72+
handCount[s.charAt(i) - 'A'] -= need;
73+
result = Math.min(result, need + dfs(s.substring(0, i) + s.substring(j), handCount));
74+
handCount[s.charAt(i) - 'A'] += need;
75+
}
8676
i = j;
8777
}
78+
return result;
79+
}
80+
81+
//remove consecutive balls longer than 3
82+
private String removeConsecutive(String board) {
83+
for (int i = 0, j = 0; j < board.length(); ++j) {
84+
if (board.charAt(j) == board.charAt(i)) {
85+
continue;
86+
}
87+
if (j - i >= 3) {
88+
return removeConsecutive(board.substring(0, i) + board.substring(j));
89+
} else {
90+
i = j;
91+
}
92+
}
93+
return board;
8894
}
89-
return board;
9095
}
9196
}

0 commit comments

Comments
 (0)