Skip to content

Commit be3bc06

Browse files
committed
✨style: Bulk processing format
1 parent 70a0c6a commit be3bc06

File tree

4 files changed

+113
-5
lines changed

4 files changed

+113
-5
lines changed

Index/DFS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
| [653. 两数之和 IV - 输入 BST](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/solution/by-ac_oier-zr4o/) | 简单 | 🤩🤩🤩🤩 |
2727
| [677. 键值映射](https://leetcode-cn.com/problems/map-sum-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/map-sum-pairs/solution/gong-shui-san-xie-jie-he-dfs-de-trie-yun-i4xa/) | 中等 | 🤩🤩🤩🤩 |
2828
| [690. 员工的重要性](https://leetcode-cn.com/problems/employee-importance/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/employee-importance/solution/gong-shui-san-xie-yi-ti-shuang-jie-di-gu-s79x/) | 简单 | 🤩🤩🤩 |
29+
| [691. 贴纸拼词](https://leetcode.cn/problems/stickers-to-spell-word/) | [LeetCode 题解链接](https://leetcode.cn/problems/stickers-to-spell-word/solution/by-ac_oier-5vv3/) | 困难 | 🤩🤩🤩🤩 |
2930
| [778. 水位上升的泳池中游泳](https://leetcode-cn.com/problems/swim-in-rising-water/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/swim-in-rising-water/solution/gong-shui-san-xie-yi-ti-shuang-jie-krusk-7c6o/) | 困难 | 🤩🤩🤩 |
3031
| [783. 二叉搜索树节点最小距离](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/solution/gong-shui-san-xie-yi-ti-san-jie-shu-de-s-7r17/) | 简单 | 🤩🤩🤩 |
3132
| [869. 重新排序得到 2 的幂](https://leetcode-cn.com/problems/reordered-power-of-2/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reordered-power-of-2/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-c-3s1e/) | 中等 | 🤩🤩🤩🤩 |

Index/记忆化搜索.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
| [494. 目标和](https://leetcode-cn.com/problems/target-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/target-sum/solution/gong-shui-san-xie-yi-ti-si-jie-dfs-ji-yi-et5b/) | 中等 | 🤩🤩🤩🤩 |
77
| [552. 学生出勤记录 II](https://leetcode-cn.com/problems/student-attendance-record-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/student-attendance-record-ii/solution/gong-shui-san-xie-yi-ti-san-jie-ji-yi-hu-fdfx/) | 困难 | 🤩🤩🤩🤩 |
88
| [576. 出界的路径数](https://leetcode-cn.com/problems/out-of-boundary-paths/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/out-of-boundary-paths/solution/gong-shui-san-xie-yi-ti-shuang-jie-ji-yi-asrz/) | 中等 | 🤩🤩🤩🤩 |
9+
| [691. 贴纸拼词](https://leetcode.cn/problems/stickers-to-spell-word/) | [LeetCode 题解链接](https://leetcode.cn/problems/stickers-to-spell-word/solution/by-ac_oier-5vv3/) | 困难 | 🤩🤩🤩🤩 |
910
| [913. 猫和老鼠](https://leetcode-cn.com/problems/cat-and-mouse/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/cat-and-mouse/solution/gong-shui-san-xie-dong-tai-gui-hua-yun-y-0bx1/) | 困难 | 🤩🤩🤩🤩 |
1011
| [1137. 第 N 个泰波那契数](https://leetcode-cn.com/problems/n-th-tribonacci-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/n-th-tribonacci-number/solution/gong-shui-san-xie-yi-ti-si-jie-die-dai-d-m1ie/) | 简单 | 🤩🤩🤩🤩 |
1112
| [1728. 猫和老鼠 II](https://leetcode.cn/problems/cat-and-mouse-ii/) | [LeetCode 题解链接](https://leetcode.cn/problems/cat-and-mouse-ii/solution/by-ac_oier-gse8/) | 困难 | 🤩🤩🤩🤩 |
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[691. 贴纸拼词](https://leetcode.cn/problems/stickers-to-spell-word/solution/by-ac_oier-5vv3/)** ,难度为 **困难**
4+
5+
Tag : 「记忆化搜索」、「DFS」、「状态压缩」、「爆搜」
6+
7+
8+
9+
我们有 $n$ 种不同的贴纸。每个贴纸上都有一个小写的英文单词。
10+
11+
您想要拼写出给定的字符串 `target` ,方法是从收集的贴纸中切割单个字母并重新排列它们。如果你愿意,你可以多次使用每个贴纸,每个贴纸的数量是无限的。
12+
13+
返回你需要拼出 `target` 的最小贴纸数量。如果任务不可能,则返回 $-1$ 。
14+
15+
注意:在所有的测试用例中,所有的单词都是从 $1000$ 个最常见的美国英语单词中随机选择的,并且 `target` 被选择为两个随机单词的连接。
16+
17+
示例 1:
18+
```
19+
输入: stickers = ["with","example","science"], target = "thehat"
20+
21+
输出:3
22+
23+
解释:
24+
我们可以使用 2 个 "with" 贴纸,和 1 个 "example" 贴纸。
25+
把贴纸上的字母剪下来并重新排列后,就可以形成目标 “thehat“ 了。
26+
此外,这是形成目标字符串所需的最小贴纸数量。
27+
```
28+
示例 2:
29+
```
30+
输入:stickers = ["notice","possible"], target = "basicbasic"
31+
32+
输出:-1
33+
34+
解释:我们不能通过剪切给定贴纸的字母来形成目标“basicbasic”。
35+
```
36+
37+
提示:
38+
* $n == stickers.length$
39+
* $1 <= n <= 50$
40+
* $1 <= stickers[i].length <= 10$
41+
* $1 <= target <= 15$
42+
* `stickers[i]` 和 `target` 由小写英文单词组成
43+
44+
---
45+
46+
### DFS + 记忆化搜索
47+
48+
为了方便,我们记 $ss = stickers$,$t = target$,其中 $t$ 的长度为 $n$。
49+
50+
我们使用一个 $state$(一个 `int` 类型变量)来代表当前 $t$ 的凑成情况:若 $t[i]$ 已被凑成,则在 $state$ 中低 $i$ 位为 $1$,否则为 $0$。
51+
52+
起始时有 `state = 0`,最终若能凑成 $t$,则有 `state = (1 << n) - 1`
53+
54+
由于每个 $ss[i]$ 可以被使用多次,因此对于一个特定的 $state$ 而言,其转换为最终的 `(1 << n) - 1` 的最小步数固定,因此我们可以使用「记忆化搜索」来避免对相同的 $state$ 进行重复搜索。
55+
56+
而在单步的搜索过程中,我们枚举每个 $ss[i]$ 来更新 $state$,假设使用某个 $ss[i]$ 得到的新状态为 $nstate$,则所有的 `dfs(nstate) + 1` 的最小值即是 $f[state]$。
57+
58+
代码:
59+
```Java
60+
class Solution {
61+
int N = 20, M = 1 << 20, INF = 50;
62+
int[] f = new int[M];
63+
String[] ss;
64+
String t;
65+
int dfs(int state) {
66+
int n = t.length();
67+
if (state == ((1 << n) - 1)) return 0;
68+
if (f[state] != -1) return f[state];
69+
int ans = INF;
70+
for (String s : ss) {
71+
int nstate = state;
72+
out:for (char c : s.toCharArray()) {
73+
for (int i = 0; i < n; i++) {
74+
if (t.charAt(i) == c && ((nstate >> i) & 1) == 0) {
75+
nstate |= (1 << i);
76+
continue out;
77+
}
78+
}
79+
}
80+
if (nstate != state) ans = Math.min(ans, dfs(nstate) + 1);
81+
}
82+
return f[state] = ans;
83+
}
84+
public int minStickers(String[] stickers, String target) {
85+
ss = stickers; t = target;
86+
Arrays.fill(f, -1);
87+
int ans = dfs(0);
88+
return ans == INF ? -1 : ans;
89+
}
90+
}
91+
```
92+
* 时间复杂度:令 $n$ 和 $m$ 分别代表字符串 `t` 的长度和数组 `ss` 的长度。共有 $2^n$ 个状态,单次状态的计算复杂度为 $$O(\sum_{i = 0}^{m - 1}ss[i].length \times n)$$。整体复杂度为 $$O(2^n \times \sum_{i = 0}^{m - 1}ss[i].length \times n)$$
93+
* 空间复杂度:$O(2^n)$
94+
95+
---
96+
97+
### 最后
98+
99+
这是我们「刷穿 LeetCode」系列文章的第 `No.691` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
100+
101+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
102+
103+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
104+
105+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
106+

LeetCode/71-80/73. 矩阵置零(中等).md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ Tag : 「模拟」
66

77

88

9-
给定一个 $m * n$ 的矩阵,如果一个元素为 $0$ ,则将其所在行和列的所有元素都设为 $0$ 。
9+
给定一个 $m \times n$ 的矩阵,如果一个元素为 $0$ ,则将其所在行和列的所有元素都设为 $0$ 。
1010

1111
请使用「原地」算法。
1212

1313
进阶:
14-
* 一个直观的解决方案是使用  $O(m * n)$ 的额外空间,但这并不是一个好的解决方案。
14+
* 一个直观的解决方案是使用  $O(m \times n)$ 的额外空间,但这并不是一个好的解决方案。
1515
* 一个简单的改进方案是使用 $O(m + n)$ 的额外空间,但这仍然不是最好的解决方案。
1616
* 你能想出一个仅使用常量空间的解决方案吗?
1717

@@ -34,13 +34,13 @@ Tag : 「模拟」
3434
* $m == matrix.length$
3535
* $n == matrix[0].length$
3636
* $1 <= m, n <= 200$
37-
* -$2^{31} <= matrix[i][j] <= 2^{31} - $
37+
* $-2^{31} <= matrix[i][j] <= 2^{31} - 1$
3838

3939
---
4040

4141
### 前言
4242

43-
由于 $O(m* n)$ 和 $O(m+n)$ 空间的解法都十分简单,无非是「同等大小的矩阵」或「与行列数量相等的标识」来记录置零信息。
43+
由于 $O(m \times n)$ 和 $O(m+n)$ 空间的解法都十分简单,无非是「同等大小的矩阵」或「与行列数量相等的标识」来记录置零信息。
4444

4545
这里着重讲解利用原矩阵的 $O(1)$ 空间解法。
4646

@@ -102,7 +102,7 @@ class Solution {
102102
}
103103
}
104104
```
105-
* 时间复杂度:$O(n*m)$
105+
* 时间复杂度:$O(n \times m)$
106106
* 空间复杂度:$O(1)$
107107

108108
---

0 commit comments

Comments
 (0)