Skip to content

Commit fc644c3

Browse files
Merge pull request SharingSource#514 from SharingSource/ac_oier
✨style: Bulk processing format
2 parents 5389e91 + 5a27416 commit fc644c3

6 files changed

+164
-8
lines changed

Index/二分.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
| [373. 查找和最小的K对数字](https://leetcode-cn.com/problems/find-k-pairs-with-smallest-sums/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-k-pairs-with-smallest-sums/solution/gong-shui-san-xie-duo-lu-gui-bing-yun-yo-pgw5/) | 中等 | 🤩🤩🤩🤩🤩 |
2424
| [374. 猜数字大小](https://leetcode-cn.com/problems/guess-number-higher-or-lower/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/guess-number-higher-or-lower/solution/gong-shui-san-xie-shi-yong-jiao-hu-han-s-tocm/) | 简单 | 🤩🤩🤩 |
2525
| [441. 排列硬币](https://leetcode-cn.com/problems/arranging-coins/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/arranging-coins/solution/gong-shui-san-xie-yi-ti-shuang-jie-shu-x-sv9o/) | 简单 | 🤩🤩🤩 |
26+
| [449. 序列化和反序列化二叉搜索树](https://leetcode.cn/problems/serialize-and-deserialize-bst/) | [LeetCode 题解链接](https://leetcode.cn/problems/serialize-and-deserialize-bst/solution/by-ac_oier-ncwn/) | 中等 | 🤩🤩🤩🤩 |
2627
| [475. 供暖器](https://leetcode-cn.com/problems/heaters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/heaters/solution/gong-shui-san-xie-er-fen-shuang-zhi-zhen-mys4/) | 中等 | 🤩🤩🤩🤩 |
2728
| [528. 按权重随机选择](https://leetcode-cn.com/problems/random-pick-with-weight/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/random-pick-with-weight/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-8bx50/) | 中等 | 🤩🤩🤩🤩 |
2829
| [540. 有序数组中的单一元素](https://leetcode-cn.com/problems/single-element-in-a-sorted-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/single-element-in-a-sorted-array/solution/gong-shui-san-xie-er-duan-xing-fen-xi-yu-17nv/) | 中等 | 🤩🤩🤩🤩 |

Index/模拟.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
| [884. 两句话中的不常见单词](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-wwam/) | 简单 | 🤩🤩🤩🤩 |
104104
| [896. 单调数列](https://leetcode-cn.com/problems/monotonic-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/monotonic-array/solution/wei-shi-yao-yi-ci-bian-li-yao-bi-liang-c-uglp/) | 简单 | 🤩🤩🤩🤩 |
105105
| [905. 按奇偶排序数组](https://leetcode-cn.com/problems/sort-array-by-parity/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sort-array-by-parity/solution/by-ac_oier-nuz7/) | 简单 | 🤩🤩🤩 |
106+
| [944. 删列造序](https://leetcode.cn/problems/delete-columns-to-make-sorted/) | [LeetCode 题解链接](https://leetcode.cn/problems/delete-columns-to-make-sorted/solution/by-ac_oier-smoz/) | 简单 | 🤩🤩🤩 |
106107
| [997. 找到小镇的法官](https://leetcode-cn.com/problems/find-the-town-judge/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-town-judge/solution/gong-shui-san-xie-jian-dan-chu-du-ru-du-5ms57/) | 简单 | 🤩🤩🤩🤩 |
107108
| [1001. 网格照明](https://leetcode-cn.com/problems/grid-illumination/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/grid-illumination/solution/gong-shui-san-xie-ha-xi-biao-xian-ying-s-s48d/) | 困难 | 🤩🤩🤩🤩 |
108109
| [1005. K 次取反后最大化的数组和](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/solution/gong-shui-san-xie-jian-dan-fen-qing-kuan-6qwu/) | 简单 | 🤩🤩🤩🤩 |

LeetCode/431-440/433. 最小基因变化(中等).md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Solution {
9191
}
9292
}
9393
```
94-
* 时间复杂度:令 $n$ 为 `bank` 的数组长度(合法状态数),将 `bank` 存入 `Set` 结构复杂度为 $O(n)$,每个状态经过一步操作最多拓展出 $C = 32$ 个新基因(共有 $8$ 个位置,每个位置有 $4$ 个选择),`BFS` 过程复杂度为 $O(C * n)$。整体复杂度为 $O(C * n)$
94+
* 时间复杂度:令 $n$ 为 `bank` 的数组长度(合法状态数),将 `bank` 存入 `Set` 结构复杂度为 $O(n)$,每个状态经过一步操作最多拓展出 $C = 32$ 个新基因(共有 $8$ 个位置,每个位置有 $4$ 个选择),`BFS` 过程复杂度为 $O(C \times n)$。整体复杂度为 $O(C \times n)$
9595
* 空间复杂度:$O(n)$
9696

9797
---
@@ -150,20 +150,20 @@ class Solution {
150150
}
151151
}
152152
```
153-
* 时间复杂度:令 $n$ 为 `bank` 的数组长度(合法状态数),将 `bank` 存入 `Set` 结构复杂度为 $O(n)$,每个状态经过一步操作最多拓展出 $C = 32$ 个新基因(共有 $8$ 个位置,每个位置有 $4$ 个选择),`BFS` 过程复杂度为 $O(C * n)$。整体复杂度为 $O(C * n)$
153+
* 时间复杂度:令 $n$ 为 `bank` 的数组长度(合法状态数),将 `bank` 存入 `Set` 结构复杂度为 $O(n)$,每个状态经过一步操作最多拓展出 $C = 32$ 个新基因(共有 $8$ 个位置,每个位置有 $4$ 个选择),`BFS` 过程复杂度为 $O(C \times n)$。整体复杂度为 $O(C \times n)$
154154
* 空间复杂度:$O(n)$
155155

156156
---
157157

158-
### A* 算法
158+
### AStar 算法
159159

160160
若不考虑 `bank` 的限制,对于一个特定状态而言,我们可以任意选择一位替换为 $4$ 类字符之一,因此对于任意状态 $x$ 而言,其与目标状态 $T$ 的「理论最小转换步数」为两者对应位置不同字符的数量,而由于存在 `bank` 限制,实际最小步数必然满足「大于等于」该理论最小转换步数。
161161

162162
基于此,我们可以计算当前状态到目标状态的「理论最小转换步数」作为启发式函数,进行启发式搜索。
163163

164164
具体的,我们使用优先队列(堆)维护所有的状态,每次优先「启发值 = 理论最小转换步数」的状态进行优先出队拓展。
165165

166-
对「A* 算法」不了解的同学可以看前置 🧀:[发挥 A* 算法最大价值的关键点](https://mp.weixin.qq.com/s?__biz=MzU4NDE3MTEyMA==&mid=2247489588&idx=1&sn=479e4c0627247ab7e20af7909f2a8b64)
166+
对「AStar 算法」不了解的同学可以看前置 🧀:[发挥 A* 算法最大价值的关键点](https://mp.weixin.qq.com/s?__biz=MzU4NDE3MTEyMA==&mid=2247489588&idx=1&sn=479e4c0627247ab7e20af7909f2a8b64)
167167

168168
代码:
169169
```Java
@@ -285,7 +285,7 @@ class Solution {
285285
}
286286
}
287287
```
288-
* 时间复杂度:令 `bank` 的长度为 $n$(即点集的数量级为 $n$),预处理出 `list` 的复杂度为 $O(n)$;建图操作的复杂度为 $O(C * n^2)$,其中 $C = 8$ 基因序列长度;`DFS` 过程由于设定了最大搜索深度,复杂度为 $O(n^2)$。整体复杂度为 $O(C * n^2)$
288+
* 时间复杂度:令 `bank` 的长度为 $n$(即点集的数量级为 $n$),预处理出 `list` 的复杂度为 $O(n)$;建图操作的复杂度为 $O(C \times n^2)$,其中 $C = 8$ 基因序列长度;`DFS` 过程由于设定了最大搜索深度,复杂度为 $O(n^2)$。整体复杂度为 $O(C \times n^2)$
289289
* 空间复杂度:最坏情况下为完全图,复杂度为 $O(n^2)$
290290

291291
---

LeetCode/441-450/449. 序列化和反序列化二叉搜索树(中等).md

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
这是 LeetCode 上的 **[449. 序列化和反序列化二叉搜索树](https://leetcode.cn/problems/serialize-and-deserialize-bst/solution/by-ac_oier-ncwn/)** ,难度为 **中等**
44

5-
Tag : 「前序遍历」、「BST」
5+
Tag : 「前序遍历」、「BST」、「二分」
66

77

88

@@ -85,7 +85,62 @@ public class Codec {
8585
}
8686
}
8787
```
88-
* 时间复杂度:令节点数量为 $n$,复杂度为 $O(n)$
88+
* 时间复杂度:令节点数量为 $n$,序列化的复杂度为 $O(n)$;反序列时由于存在「找第一个比头结点值大的位置」操作,每个节点可能被扫描多次,扫描次数与当前节点所在的深度相关,最坏情况下为一条往左下方的链,复杂度为 $O(n^2)$
89+
* 空间复杂度:$O(n)$
90+
91+
---
92+
93+
### 二分优化
94+
95+
在解法一中的「反序列操作」操作的瓶颈在于需要「找第一个比头结点值大的位置」。
96+
97+
假设连续段 $s[l, r]$ 代表某棵子树的话,由于我们是采用「前序遍历」的方式生成 `s`,因此头结点必然是 $s[l]$,而对于头结点的左右子树,必然是连续两段(先左再右)的形式存储在 $[l + 1, r]$ 中,同时由于该子树是 BST,因此这连续两段必然满足「前一段(左子树)小于 $t$」和「后一段(右子树)大于 $t$」。
98+
99+
即具有「二段性」,因此「找第一个比头结点值大的位置」可用「二分」实现。
100+
101+
代码:
102+
```Java
103+
public class Codec {
104+
public String serialize(TreeNode root) {
105+
if (root == null) return null;
106+
List<String> list = new ArrayList<>();
107+
dfs1(root, list);
108+
int n = list.size();
109+
StringBuilder sb = new StringBuilder();
110+
for (int i = 0; i < n; i++) {
111+
sb.append(list.get(i));
112+
if (i != n - 1) sb.append(",");
113+
}
114+
return sb.toString();
115+
}
116+
void dfs1(TreeNode root, List<String> list) {
117+
if (root == null) return ;
118+
list.add(String.valueOf(root.val));
119+
dfs1(root.left, list);
120+
dfs1(root.right, list);
121+
}
122+
public TreeNode deserialize(String s) {
123+
if (s == null) return null;
124+
String[] ss = s.split(",");
125+
return dfs2(0, ss.length - 1, ss);
126+
}
127+
TreeNode dfs2(int l, int r, String[] ss) {
128+
if (l > r) return null;
129+
int ll = l + 1, rr = r, t = Integer.parseInt(ss[l]);
130+
while (ll < rr) {
131+
int mid = ll + rr >> 1;
132+
if (Integer.parseInt(ss[mid]) > t) rr = mid;
133+
else ll = mid + 1;
134+
}
135+
if (Integer.parseInt(ss[rr]) <= t) rr++;
136+
TreeNode ans = new TreeNode(t);
137+
ans.left = dfs2(l + 1, rr - 1, ss);
138+
ans.right = dfs2(rr, r, ss);
139+
return ans;
140+
}
141+
}
142+
```
143+
* 时间复杂度:令节点数量为 $n$,序列化的复杂度为 $O(n)$;反序列时由于存在「找第一个比头结点值大的位置」操作,最坏情况下为一条往左下方的链,该操作采用「二分」,复杂度为 $O(\log{n})$,整体复杂度为 $O(n\log{n})$
89144
* 空间复杂度:$O(n)$
90145

91146
---

LeetCode/911-920/913. 猫和老鼠(困难).md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Tag : 「动态规划」、「记忆化搜索」
5757

5858
数据范围只有 $50$,使得本题的难度大大降低。
5959

60-
**定义 $f[k][i][j]$ 为当前进行了 $k$ 步,老鼠所在位置为 $i$,猫所在的位置为 $j$ 时,最终的获胜情况($0$ 为平局,$1$ 和 $2$ 分别代表老鼠和猫获胜),起始我们让所有的 $f[i][j][k] = -1$(为无效值),最终答案为 $f[0][1][2]$。**
60+
**定义 $f[k][i][j]$ 为当前进行了 $k$ 步,老鼠所在位置为 $i$,猫所在的位置为 $j$ 时,最终的获胜情况($0$ 为平局,$1$ 和 $2$ 分别代表老鼠和猫获胜),起始我们让所有的 $f[k][i][j] = -1$(为无效值),最终答案为 $f[0][1][2]$。**
6161

6262
不失一般性的考虑 $f[i][j][k]$ 该如何转移,根据题意,将当前所在位置 $i$ 和 $j$ 结合「游戏结束,判定游戏」的规则来分情况讨论:
6363

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[944. 删列造序](https://leetcode.cn/problems/delete-columns-to-make-sorted/solution/by-ac_oier-smoz/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给你由 $n$ 个小写字母字符串组成的数组 `strs`,其中每个字符串长度相等。
10+
11+
这些字符串可以每个一行,排成一个网格。例如,`strs = ["abc", "bce", "cae"]` 可以排列为:
12+
```
13+
abc
14+
bce
15+
cae
16+
```
17+
你需要找出并删除 不是按字典序升序排列的 列。在上面的例子(下标从 $0$ 开始)中,列 $0$(`'a', 'b', 'c'`)和列 $2$(`'c', 'e', 'e'`)都是按升序排列的,而列 $1$(`'b', 'c', 'a'`)不是,所以要删除列 $1$ 。
18+
19+
返回你需要删除的列数。
20+
21+
示例 1:
22+
```
23+
输入:strs = ["cba","daf","ghi"]
24+
25+
输出:1
26+
27+
解释:网格示意如下:
28+
cba
29+
daf
30+
ghi
31+
列 0 和列 2 按升序排列,但列 1 不是,所以只需要删除列 1 。
32+
```
33+
示例 2:
34+
```
35+
输入:strs = ["a","b"]
36+
37+
输出:0
38+
39+
解释:网格示意如下:
40+
a
41+
b
42+
只有列 0 这一列,且已经按升序排列,所以不用删除任何列。
43+
```
44+
示例 3:
45+
```
46+
输入:strs = ["zyx","wvu","tsr"]
47+
48+
输出:3
49+
50+
解释:网格示意如下:
51+
zyx
52+
wvu
53+
tsr
54+
所有 3 列都是非升序排列的,所以都要删除。
55+
```
56+
57+
提示:
58+
* $n == strs.length$
59+
* $1 <= n <= 100$
60+
* $1 <= strs[i].length <= 1000$
61+
* `strs[i]` 由小写英文字母组成
62+
63+
---
64+
65+
### 模拟
66+
67+
根据题意进行模拟即可。
68+
69+
代码:
70+
```Java
71+
class Solution {
72+
public int minDeletionSize(String[] strs) {
73+
int n = strs.length, m = strs[0].length(), ans = 0;
74+
out:for (int i = 0; i < m; i++) {
75+
for (int j = 0, cur = -1; j < n; j++) {
76+
int t = (int) strs[j].charAt(i);
77+
if (t < cur && ++ans >= 0) continue out;
78+
cur = t;
79+
}
80+
}
81+
return ans;
82+
}
83+
}
84+
```
85+
* 时间复杂度:$O(n \times m)$
86+
* 空间复杂度:$O(1)$
87+
88+
---
89+
90+
### 最后
91+
92+
这是我们「刷穿 LeetCode」系列文章的第 `No.942` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
93+
94+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
95+
96+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
97+
98+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
99+

0 commit comments

Comments
 (0)