@@ -38,54 +38,59 @@ Each time, you may choose a ball in your hand, and insert it into the row (inclu
38
38
*/
39
39
public class _488 {
40
40
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 {
43
42
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
+ */
45
47
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"
54
49
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' ];
71
54
}
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 ;
73
57
}
74
- return result ;
75
- }
76
58
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 ;
82
63
}
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
+ }
86
76
i = j ;
87
77
}
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 ;
88
94
}
89
- return board ;
90
95
}
91
96
}
0 commit comments