|
6 | 6 | public class Code02_StickersToSpellWord { |
7 | 7 |
|
8 | 8 | public static int minStickers1(String[] stickers, String target) { |
| 9 | + |
9 | 10 | int n = stickers.length; |
10 | | -int[][] map = new int[n][26]; |
11 | | -HashMap<String, Integer> dp = new HashMap<>(); |
| 11 | + |
| 12 | +int[][] map = new int[n][26];// stickers -> [26] [26] [26] |
12 | 13 | for (int i = 0; i < n; i++) { |
13 | 14 | char[] str = stickers[i].toCharArray(); |
14 | 15 | for (char c : str) { |
15 | 16 | map[i][c - 'a']++; |
16 | 17 | } |
17 | 18 | } |
| 19 | +HashMap<String, Integer> dp = new HashMap<>(); |
18 | 20 | dp.put("", 0); |
19 | 21 | return process1(dp, map, target); |
20 | 22 | } |
21 | 23 |
|
22 | 24 | // dp 傻缓存,如果t已经算过了,直接返回dp中的值 |
23 | 25 | // t 剩余的目标 |
24 | 26 | // 0..N每一个字符串所含字符的词频统计 |
25 | | -public static int process1(HashMap<String, Integer> dp, int[][] map, String t) { |
26 | | -if (dp.containsKey(t)) { |
27 | | -return dp.get(t); |
| 27 | +// 返回值是-1,map 中的贴纸 怎么都无法rest |
| 28 | +public static int process1( |
| 29 | +HashMap<String, Integer> dp, |
| 30 | +int[][] map, |
| 31 | +String rest) { |
| 32 | +if (dp.containsKey(rest)) { |
| 33 | +return dp.get(rest); |
28 | 34 | } |
29 | | -int ans = Integer.MAX_VALUE; |
30 | | -int n = map.length; |
31 | | -int[] tmap = new int[26]; |
32 | | -char[] target = t.toCharArray(); |
| 35 | +// 以下就是正式的递归调用过程 |
| 36 | +int ans = Integer.MAX_VALUE; // ans -> 搞定rest,使用的最少的贴纸数量 |
| 37 | +int n = map.length; // N种贴纸 |
| 38 | +int[] tmap = new int[26]; // tmap 去替代 rest |
| 39 | +char[] target = rest.toCharArray(); |
33 | 40 | for (char c : target) { |
34 | 41 | tmap[c - 'a']++; |
35 | 42 | } |
36 | 43 | for (int i = 0; i < n; i++) { |
| 44 | +// 枚举当前第一张贴纸是谁? |
37 | 45 | if (map[i][target[0] - 'a'] == 0) { |
38 | 46 | continue; |
39 | 47 | } |
40 | 48 | StringBuilder sb = new StringBuilder(); |
41 | | -for (int j = 0; j < 26; j++) { |
| 49 | +// i 贴纸, j 枚举a~z字符 |
| 50 | +for (int j = 0; j < 26; j++) { // |
42 | 51 | if (tmap[j] > 0) { // j这个字符是target需要的 |
43 | 52 | for (int k = 0; k < Math.max(0, tmap[j] - map[i][j]); k++) { |
44 | 53 | sb.append((char) ('a' + j)); |
45 | 54 | } |
46 | 55 | } |
47 | 56 | } |
| 57 | +// sb -> i |
48 | 58 | String s = sb.toString(); |
49 | 59 | int tmp = process1(dp, map, s); |
50 | 60 | if (tmp != -1) { |
51 | 61 | ans = Math.min(ans, 1 + tmp); |
52 | 62 | } |
53 | 63 | } |
54 | | -dp.put(t, ans == Integer.MAX_VALUE ? -1 : ans); |
55 | | -return dp.get(t); |
| 64 | +// ans 系统最大 rest |
| 65 | +dp.put(rest, ans == Integer.MAX_VALUE ? -1 : ans); |
| 66 | +return dp.get(rest); |
56 | 67 | } |
57 | 68 |
|
58 | 69 | public static int minStickers2(String[] stickers, String target) { |
@@ -128,5 +139,14 @@ public static int process2(int[][] map, int i, int[] tmap, HashMap<String, Integ |
128 | 139 | dp.put(key, ans); |
129 | 140 | return ans; |
130 | 141 | } |
| 142 | + |
| 143 | +public static void main(String[] args) { |
| 144 | +String[] arr = {"aaaa","bbaa","ccddd"}; |
| 145 | +String str = "abcccccdddddbbbaaaaa"; |
| 146 | +System.out.println(minStickers1(arr, str)); |
| 147 | +System.out.println(minStickers2(arr, str)); |
| 148 | + |
| 149 | + |
| 150 | +} |
131 | 151 |
|
132 | 152 | } |
0 commit comments