|
1 | 1 | package g0401_0500.s0488_zuma_game; |
2 | 2 |
|
3 | 3 | // #Hard #String #Dynamic_Programming #Breadth_First_Search #Memoization |
4 | | -// #2022_07_21_Time_1593_ms_(37.71%)_Space_234.5_MB_(13.12%) |
| 4 | +// #2022_08_19_Time_370_ms_(90.36%)_Space_95.2_MB_(69.88%) |
5 | 5 |
|
6 | | -import java.util.ArrayDeque; |
7 | | -import java.util.Deque; |
8 | 6 | import java.util.HashMap; |
| 7 | +import java.util.Map; |
9 | 8 |
|
10 | 9 | public class Solution { |
11 | | - private HashMap<String, Integer> map = new HashMap<>(); |
12 | | - |
13 | | - private String removeConsecutiveThreeOrMoreBalls(String board) { |
14 | | - Deque<Character> st = new ArrayDeque<>(); |
15 | | - int n = board.length(); |
16 | | - for (int i = 0; i < n; i++) { |
17 | | - char ch = board.charAt(i); |
18 | | - st.push(ch); |
19 | | - if (st.size() >= 3 && (i == n - 1 || ch != board.charAt(i + 1))) { |
20 | | - char a = st.pop(); |
21 | | - char b = st.pop(); |
22 | | - char c = st.pop(); |
23 | | - if (a == b && b == c) { |
24 | | - while (!st.isEmpty() && st.peek() == a) { |
25 | | - st.pop(); |
26 | | - } |
27 | | - } else { |
28 | | - st.push(c); |
29 | | - st.push(b); |
30 | | - st.push(a); |
31 | | - } |
32 | | - } |
33 | | - } |
34 | | - StringBuilder res = new StringBuilder(); |
35 | | - while (!st.isEmpty()) { |
36 | | - res.append(st.pop()); |
37 | | - } |
38 | | - return res.reverse().toString(); |
| 10 | + public int findMinStep(String board, String hand) { |
| 11 | + return dfs(board, hand); |
39 | 12 | } |
40 | 13 |
|
41 | | - private String ss(String s, int i, int j) { |
42 | | - return s.substring(i, j); |
| 14 | + private int dfs(String board, String hand) { |
| 15 | + return findMinStepDp(board, hand, new HashMap<>()); |
43 | 16 | } |
44 | 17 |
|
45 | | - private int solve(String board, String hand) { |
46 | | - String key = board + "#" + hand; |
47 | | - if (map.containsKey(key)) { |
48 | | - return map.get(key); |
| 18 | + private int findMinStepDp(String board, String hand, Map<String, Map<String, Integer>> dp) { |
| 19 | + if (board.length() == 0) { |
| 20 | + return 0; |
| 21 | + } |
| 22 | + if (hand.length() == 0) { |
| 23 | + return -1; |
49 | 24 | } |
50 | | - board = removeConsecutiveThreeOrMoreBalls(board); |
51 | | - int ans = 100; |
52 | | - int n = board.length(); |
53 | | - int m = hand.length(); |
54 | | - if (n == 0 || m == 0) { |
55 | | - map.put(key, n == 0 ? 0 : 100); |
56 | | - return n == 0 ? 0 : 100; |
| 25 | + if (dp.get(board) != null && dp.get(board).get(hand) != null) { |
| 26 | + return dp.get(board).get(hand); |
57 | 27 | } |
58 | | - for (int i = 0; i < hand.length(); i++) { |
59 | | - for (int j = 0; j < n; j++) { |
60 | | - if (board.charAt(j) == hand.charAt(i) |
61 | | - || (j < n - 1 && board.charAt(j) == board.charAt(j + 1))) { |
62 | | - ans = |
63 | | - Math.min( |
64 | | - ans, |
65 | | - 1 |
66 | | - + solve( |
67 | | - ss(board, 0, j + 1) |
68 | | - + hand.charAt(i) |
69 | | - + ss(board, j + 1, n), |
70 | | - ss(hand, 0, i) + ss(hand, i + 1, m))); |
| 28 | + int min = -1; |
| 29 | + for (int i = 0; i <= board.length(); i++) { |
| 30 | + for (int j = 0; j < hand.length(); j++) { |
| 31 | + if ((j == 0 || hand.charAt(j) != hand.charAt(j - 1)) |
| 32 | + && (i == 0 || board.charAt(i - 1) != hand.charAt(j)) |
| 33 | + && ((i < board.length() && board.charAt(i) == hand.charAt(j)) |
| 34 | + || (i > 0 |
| 35 | + && i < board.length() |
| 36 | + && board.charAt(i - 1) == board.charAt(i) |
| 37 | + && board.charAt(i) != hand.charAt(j)))) { |
| 38 | + |
| 39 | + StringBuilder newS = new StringBuilder(board); |
| 40 | + newS.insert(i, hand.charAt(j)); |
| 41 | + int sR = |
| 42 | + findMinStepDp( |
| 43 | + removeRepeated(newS.toString()), |
| 44 | + hand.substring(0, j) + hand.substring(j + 1, hand.length()), |
| 45 | + dp); |
| 46 | + if (sR != -1) { |
| 47 | + min = min == -1 ? sR + 1 : Integer.min(min, sR + 1); |
| 48 | + } |
71 | 49 | } |
72 | 50 | } |
73 | 51 | } |
74 | | - map.put(key, ans); |
75 | | - return ans; |
| 52 | + dp.putIfAbsent(board, new HashMap<>()); |
| 53 | + dp.get(board).put(hand, min); |
| 54 | + return min; |
76 | 55 | } |
77 | 56 |
|
78 | | - public int findMinStep(String board, String hand) { |
79 | | - int ans = solve(board, hand); |
80 | | - return ans >= 100 ? -1 : ans; |
| 57 | + private String removeRepeated(String original) { |
| 58 | + int count = 1; |
| 59 | + int i = 1; |
| 60 | + while (i < original.length()) { |
| 61 | + if (original.charAt(i) == original.charAt(i - 1)) { |
| 62 | + count++; |
| 63 | + i++; |
| 64 | + } else { |
| 65 | + if (count >= 3) { |
| 66 | + return removeRepeated( |
| 67 | + original.substring(0, i - count) |
| 68 | + + original.substring(i, original.length())); |
| 69 | + } else { |
| 70 | + count = 1; |
| 71 | + i++; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + if (count >= 3) { |
| 76 | + return removeRepeated(original.substring(0, original.length() - count)); |
| 77 | + } else { |
| 78 | + return original; |
| 79 | + } |
81 | 80 | } |
82 | 81 | } |
0 commit comments