Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Index/图论 BFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
| [815. 公交路线](https://leetcode-cn.com/problems/bus-routes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/bus-routes/solution/gong-shui-san-xie-yi-ti-shuang-jie-po-su-1roh/) | 困难 | 🤩🤩🤩🤩 |
| [847. 访问所有节点的最短路径](https://leetcode-cn.com/problems/shortest-path-visiting-all-nodes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/shortest-path-visiting-all-nodes/solution/gong-shui-san-xie-yi-ti-shuang-jie-bfs-z-6p2k/) | 困难 | 🤩🤩🤩🤩🤩 |
| [863. 二叉树中所有距离为 K 的结点](https://leetcode-cn.com/problems/all-nodes-distance-k-in-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/all-nodes-distance-k-in-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-jian-x6hak/) | 中等 | 🤩🤩🤩🤩 |
| [864. 获取所有钥匙的最短路径](https://leetcode.cn/problems/shortest-path-to-get-all-keys/) | [LeetCode 题解链接](https://leetcode.cn/problems/shortest-path-to-get-all-keys/solution/by-ac_oier-5gxc/) | 困难 | 🤩🤩🤩🤩🤩 |
| [909. 蛇梯棋](https://leetcode-cn.com/problems/snakes-and-ladders/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/snakes-and-ladders/solution/gong-shui-san-xie-bfs-mo-ni-by-ac_oier-woh6/) | 中等 | 🤩🤩🤩🤩 |
| [1020. 飞地的数量](https://leetcode-cn.com/problems/number-of-enclaves/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-enclaves/solution/gong-shui-san-xie-bing-cha-ji-dfs-yun-yo-oyh1/) | 中等 | 🤩🤩🤩 |
| [1034. 边界着色](https://leetcode-cn.com/problems/coloring-a-border/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/coloring-a-border/solution/gong-shui-san-xie-tu-lun-sou-suo-zhuan-t-snvw/) | 中等 | 🤩🤩🤩🤩 |
Expand Down
1 change: 1 addition & 0 deletions Index/模拟.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
| [747. 至少是其他数字两倍的最大数](https://leetcode-cn.com/problems/largest-number-at-least-twice-of-others/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/largest-number-at-least-twice-of-others/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-8179/) | 简单 | 🤩🤩🤩🤩🤩 |
| [748. 最短补全词](https://leetcode-cn.com/problems/shortest-completing-word/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/shortest-completing-word/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-j-x4ao/) | 简单 | 🤩🤩🤩🤩 |
| [762. 二进制表示中质数个计算置位](https://leetcode-cn.com/problems/prime-number-of-set-bits-in-binary-representation/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/prime-number-of-set-bits-in-binary-representation/solution/by-ac_oier-w50x/) | 简单 | 🤩🤩🤩🤩 |
| [764. 最大加号标志](https://leetcode.cn/problems/largest-plus-sign/) | [LeetCode 题解链接](https://leetcode.cn/problems/largest-plus-sign/solution/by-ac_oier-q932/) | 中等 | 🤩🤩🤩 |
| [766. 托普利茨矩阵](https://leetcode-cn.com/problems/toeplitz-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/toeplitz-matrix/solution/cong-ci-pan-du-qu-cheng-ben-fen-xi-liang-f20w/) | 简单 | 🤩🤩🤩 |
| [769. 最多能完成排序的块](https://leetcode.cn/problems/max-chunks-to-make-sorted/) | [LeetCode 题解链接](https://leetcode.cn/problems/max-chunks-to-make-sorted/solution/by-ac_oier-4uny/) | 中等 | 🤩🤩🤩🤩🤩 |
| [788. 旋转数字](https://leetcode.cn/problems/rotated-digits/) | [LeetCode 题解链接](https://leetcode.cn/problems/rotated-digits/solution/by-ac_oier-9qpw/) | 中等 | 🤩🤩🤩🤩 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,7 @@ Tag : 「DFS」、「二进制枚举」、「模拟退火」、「随机化」

### 基本分析

根据题意,可以将本题看做一类特殊的「数独问题」:在给定的 `arr` 字符数组中选择,尽可能多的覆盖一个 $1 * 26$ 的矩阵。

对于此类「精确覆盖」问题,换个角度也可以看做「组合问题」。

通常有几种做法:`DFS`、剪枝 `DFS`、二进制枚举、模拟退火、`DLX`。

其中一头一尾解法过于简单和困难,有兴趣的同学自行了解与实现。

---

### 基本分析

根据题意,可以将本题看做一类特殊的「数独问题」:在给定的 `arr` 字符数组中选择,尽可能多的覆盖一个 $1 * 26$ 的矩阵。
根据题意,可以将本题看做一类特殊的「数独问题」:在给定的 `arr` 字符数组中选择,尽可能多的覆盖一个 $1 \times 26$ 的矩阵。

对于此类「精确覆盖」问题,换个角度也可以看做「组合问题」。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ Tag : 「DFS」、「模拟退火」、「启发式搜索」、「随机化」
```

提示:
* 1 <= k <= jobs.length <= 12
* 1 <= jobs[i] <= $10^7$
* $1 <= k <= jobs.length <= 12$
* $1 <= jobs[i] <= 10^7$

---

Expand Down Expand Up @@ -185,6 +185,8 @@ class Solution {
2. 如果温度下降(交换后的序列更优),进入下一次迭代
3. 如果温度上升(交换前的序列更优),以「一定的概率」恢复现场(再交换回来)

> 对于一个能够运用模拟退火求解的问题,最核心的是如何实现 `calc` 方法(即如何定义一个具体方案的得分),其余均为模板内容。

代码:
```Java
class Solution {
Expand Down Expand Up @@ -249,18 +251,23 @@ class Solution {
}
}
```
* 时间复杂度:启发式搜索不讨论时空复杂度
* 空间复杂度:启发式搜索不讨论时空复杂度

---

### 我猜你问

**Q0. 模拟退火有何风险?**

随机算法,会面临 `WA` 和 `TLE` 风险。

**Q1. 模拟退火中的参数如何敲定的?**

根据经验猜的,然后提交。根据结果是 `WA` 还是 `TLE` 来决定之后的调参方向。如果是 `WA` 说明部分数据落到了「局部最优」或者尚未达到「全局最优」。

**Q2. 参数如何调整?**

如果是 `WA` 了,一般我是优先调大 fa 参数,使降温变慢,来变相增加迭代次数;如果是 `TLE` 了,一般是优先调小 fa 参数,使降温变快,减小迭代次数。总迭代参数 `N` 也是同理。

可以简单理解调大 fa 代表将「大步」改为「baby step」,防止越过全局最优,同时增加总执行步数。
Expand Down Expand Up @@ -291,7 +298,6 @@ class Solution {

**本质上,我们并没有主动的否决某些方案(也就是我们并没有改动递归树),我们只是调整了搜索顺序来剪枝掉了一些「必然不是最优」的搜索路径。**


---

### 最后
Expand Down
152 changes: 152 additions & 0 deletions LeetCode/761-770/764. 最大加号标志(中等).md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
### 题目描述

这是 LeetCode 上的 **[764. 最大加号标志](https://leetcode.cn/problems/largest-plus-sign/solution/by-ac_oier-q932/)** ,难度为 **中等**。

Tag : 「模拟」、「预处理」



在一个 $n \times n$ 的矩阵 `grid` 中,除了在数组 `mines` 中给出的元素为 `0`,其他每个元素都为 `1`。$mines[i] = [x_i, y_i]$ 表示 $grid[x_i][y_i] = 0$。

返回  `grid` 中包含 `1` 的最大的 轴对齐 加号标志的阶数 。如果未找到加号标志,则返回 `0` 。

一个 `k` 阶由 `1` 组成的 “轴对称”加号标志 具有中心网格 $grid[r][c] = 1$ ,以及 `4` 个从中心向上、向下、向左、向右延伸,长度为 `k-1`,由 `1` 组成的臂。注意,只有加号标志的所有网格要求为 `1` ,别的网格可能为 `0` 也可能为 `1` 。

示例 1:
![](https://assets.leetcode.com/uploads/2021/06/13/plus1-grid.jpg)
```
输入: n = 5, mines = [[4, 2]]

输出: 2

解释: 在上面的网格中,最大加号标志的阶只能是2。一个标志已在图中标出。
```
示例 2:
![](https://assets.leetcode.com/uploads/2021/06/13/plus2-grid.jpg)
```
输入: n = 1, mines = [[0, 0]]

输出: 0

解释: 没有加号标志,返回 0 。
```

提示:
* $1 <= n <= 500$
* $1 <= mines.length <= 5000$
* $0 <= x_i, y_i < n$
* 每一对 $(x_i, y_i)$ 都 不重复

---

### 预处理 + 模拟

假设点 $(x, y)$ 能够取得最大长度 $k$,我们知道 $k$ 取决于以点 $(x, y)$ 为起点,四联通方向中「最短的连续 $1$ 的长度」。

基于此,我们可以建立四个大小为 $n \times n$ 的矩阵(二维数组)`a`、`b`、`c` 和 `d` 分别代表四个方向连续 $1$ 的前缀数:

![image.png](https://pic.leetcode.cn/1667958744-jszheo-image.png)

数据范围为 $500$,预处理前缀数组复杂度为 $O(n^2)$,统计答案复杂度为 $O(n^2)$,时间复杂度没有问题。

再考虑空间,建立四个方向的前缀数组所需空间为 $500 \times 500 \times 4 = 10^6$,即使加上原矩阵 `g` 也不会有 `MLE` 风险,空间复杂度也没有问题。

Java 代码:
```Java
class Solution {
public int orderOfLargestPlusSign(int n, int[][] mines) {
int[][] g = new int[n + 10][n + 10];
for (int i = 1; i <= n; i++) Arrays.fill(g[i], 1);
for (int[] info : mines) g[info[0] + 1][info[1] + 1] = 0;
int[][] a = new int[n + 10][n + 10], b = new int[n + 10][n + 10], c = new int[n + 10][n + 10], d = new int[n + 10][n + 10];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (g[i][j] == 1) {
a[i][j] = a[i - 1][j] + 1;
b[i][j] = b[i][j - 1] + 1;
}
if (g[n + 1 - i][n + 1 - j] == 1) {
c[n + 1 - i][n + 1 - j] = c[n + 2 - i][n + 1 - j] + 1;
d[n + 1 - i][n + 1 - j] = d[n + 1 - i][n + 2 - j] + 1;
}
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
ans = Math.max(ans, Math.min(Math.min(a[i][j], b[i][j]), Math.min(c[i][j], d[i][j])));
}
}
return ans;
}
}
```
TypeScript 代码:
```TypeScript
function orderOfLargestPlusSign(n: number, mines: number[][]): number {
function getMat(x: number, y: number, val: number): number[][] {
const ans = new Array<Array<number>>(x)
for (let i = 0; i < x; i++) ans[i] = new Array<number>(y).fill(val)
return ans
}
const g = getMat(n + 10, n + 10, 1)
for (const info of mines) g[info[0] + 1][info[1] + 1] = 0
const a = getMat(n + 10, n + 10, 0), b = getMat(n + 10, n + 10, 0), c = getMat(n + 10, n + 10, 0), d = getMat(n + 10, n + 10, 0)
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= n; j++) {
if (g[i][j] == 1) {
a[i][j] = a[i - 1][j] + 1
b[i][j] = b[i][j - 1] + 1
}
if (g[n + 1 - i][n + 1 - j] == 1) {
c[n + 1 - i][n + 1 - j] = c[n + 2 - i][n + 1 - j] + 1
d[n + 1 - i][n + 1 - j] = d[n + 1 - i][n + 2 - j] + 1
}
}
}
let ans = 0
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= n; j++) {
ans = Math.max(ans, Math.min(Math.min(a[i][j], b[i][j]), Math.min(c[i][j], d[i][j])))
}
}
return ans
}
```
Python 代码:
```Python
class Solution:
def orderOfLargestPlusSign(self, n: int, mines: List[List[int]]) -> int:
g = [[1] * (n + 10) for _ in range(n + 10)]
for x, y in mines:
g[x + 1][y + 1] = 0
a, b, c, d = [[0] * (n + 10) for _ in range(n + 10)], [[0] * (n + 10) for _ in range(n + 10)], [[0] * (n + 10) for _ in range(n + 10)], [[0] * (n + 10) for _ in range(n + 10)]
for i in range(1, n + 1):
for j in range(1, n + 1):
if g[i][j] == 1:
a[i][j] = a[i - 1][j] + 1
b[i][j] = b[i][j - 1] + 1
if g[n + 1 - i][n + 1 - j] == 1:
c[n + 1 - i][n + 1 - j] = c[n + 2 - i][n + 1 - j] + 1
d[n + 1 - i][n + 1 - j] = d[n + 1 - i][n + 2 - j] + 1
ans = 0
for i in range(1, n + 1):
for j in range(1, n + 1):
ans = max(ans, min(min(a[i][j], b[i][j]), min(c[i][j], d[i][j])))
return ans
```
* 时间复杂度:$O(n^2)$
* 空间复杂度:$O(n^2)$

---

### 最后

这是我们「刷穿 LeetCode」系列文章的第 `No.764` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。

Loading