Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f227809

Browse files
✨feat: add 1723
1 parent 452fb13 commit f227809

File tree

4 files changed

+163
-16
lines changed

4 files changed

+163
-16
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
| [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/) | 简单 | 🤩🤩🤩🤩🤩 |
101101
| [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/) | 简单 | 🤩🤩🤩🤩 |
102102
| [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/) | 简单 | 🤩🤩🤩🤩 |
103+
| [764. 最大加号标志](https://leetcode.cn/problems/largest-plus-sign/) | [LeetCode 题解链接](https://leetcode.cn/problems/largest-plus-sign/solution/by-ac_oier-q932/) | 中等 | 🤩🤩🤩 |
103104
| [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/) | 简单 | 🤩🤩🤩 |
104105
| [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/) | 中等 | 🤩🤩🤩🤩🤩 |
105106
| [788. 旋转数字](https://leetcode.cn/problems/rotated-digits/) | [LeetCode 题解链接](https://leetcode.cn/problems/rotated-digits/solution/by-ac_oier-9qpw/) | 中等 | 🤩🤩🤩🤩 |

‎LeetCode/1231-1240/1239. 串联字符串的最大长度(中等).md‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,7 @@ Tag : 「DFS」、「二进制枚举」、「模拟退火」、「随机化」
4343

4444
### 基本分析
4545

46-
根据题意,可以将本题看做一类特殊的「数独问题」:在给定的 `arr` 字符数组中选择,尽可能多的覆盖一个 1ドル * 26$ 的矩阵。
47-
48-
对于此类「精确覆盖」问题,换个角度也可以看做「组合问题」。
49-
50-
通常有几种做法:`DFS`、剪枝 `DFS`、二进制枚举、模拟退火、`DLX`
51-
52-
其中一头一尾解法过于简单和困难,有兴趣的同学自行了解与实现。
53-
54-
---
55-
56-
### 基本分析
57-
58-
根据题意,可以将本题看做一类特殊的「数独问题」:在给定的 `arr` 字符数组中选择,尽可能多的覆盖一个 1ドル * 26$ 的矩阵。
46+
根据题意,可以将本题看做一类特殊的「数独问题」:在给定的 `arr` 字符数组中选择,尽可能多的覆盖一个 1ドル \times 26$ 的矩阵。
5947

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

‎LeetCode/1721-1730/1723. 完成所有工作的最短时间(困难).md‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ Tag : 「DFS」、「模拟退火」、「启发式搜索」、「随机化」
3838
```
3939

4040
提示:
41-
* 1 <= k <= jobs.length <= 12
42-
* 1 <= jobs[i] <= $10^7$
41+
* $1 <= k <= jobs.length <= 12$
42+
* $1 <= jobs[i] <= 10^7$
4343

4444
---
4545

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

188+
> 对于一个能够运用模拟退火求解的问题,最核心的是如何实现 `calc` 方法(即如何定义一个具体方案的得分),其余均为模板内容。
189+
188190
代码:
189191
```Java
190192
class Solution {
@@ -249,18 +251,23 @@ class Solution {
249251
}
250252
}
251253
```
254+
* 时间复杂度:启发式搜索不讨论时空复杂度
255+
* 空间复杂度:启发式搜索不讨论时空复杂度
252256

253257
---
254258

255259
### 我猜你问
256260

257261
**Q0. 模拟退火有何风险?**
262+
258263
随机算法,会面临 `WA``TLE` 风险。
259264

260265
**Q1. 模拟退火中的参数如何敲定的?**
266+
261267
根据经验猜的,然后提交。根据结果是 `WA` 还是 `TLE` 来决定之后的调参方向。如果是 `WA` 说明部分数据落到了「局部最优」或者尚未达到「全局最优」。
262268

263269
**Q2. 参数如何调整?**
270+
264271
如果是 `WA` 了,一般我是优先调大 fa 参数,使降温变慢,来变相增加迭代次数;如果是 `TLE` 了,一般是优先调小 fa 参数,使降温变快,减小迭代次数。总迭代参数 `N` 也是同理。
265272

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

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

294-
295301
---
296302

297303
### 最后
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[764. 最大加号标志](https://leetcode.cn/problems/largest-plus-sign/solution/by-ac_oier-q932/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」、「预处理」
6+
7+
8+
9+
在一个 $n \times n$ 的矩阵 `grid` 中,除了在数组 `mines` 中给出的元素为 `0`,其他每个元素都为 `1`。$mines[i] = [x_i, y_i]$ 表示 $grid[x_i][y_i] = 0$
10+
11+
返回 `grid` 中包含 `1` 的最大的 轴对齐 加号标志的阶数 。如果未找到加号标志,则返回 `0`
12+
13+
一个 `k` 阶由 `1` 组成的 "轴对称"加号标志 具有中心网格 $grid[r][c] = 1$ ,以及 `4` 个从中心向上、向下、向左、向右延伸,长度为 `k-1`,由 `1` 组成的臂。注意,只有加号标志的所有网格要求为 `1` ,别的网格可能为 `0` 也可能为 `1`
14+
15+
示例 1:
16+
![](https://assets.leetcode.com/uploads/2021/06/13/plus1-grid.jpg)
17+
```
18+
输入: n = 5, mines = [[4, 2]]
19+
20+
输出: 2
21+
22+
解释: 在上面的网格中,最大加号标志的阶只能是2。一个标志已在图中标出。
23+
```
24+
示例 2:
25+
![](https://assets.leetcode.com/uploads/2021/06/13/plus2-grid.jpg)
26+
```
27+
输入: n = 1, mines = [[0, 0]]
28+
29+
输出: 0
30+
31+
解释: 没有加号标志,返回 0 。
32+
```
33+
34+
提示:
35+
* 1ドル <= n <= 500$
36+
* 1ドル <= mines.length <= 5000$
37+
* 0ドル <= x_i, y_i < n$
38+
* 每一对 $(x_i, y_i)$ 都 不重复
39+
40+
---
41+
42+
### 预处理 + 模拟
43+
44+
假设点 $(x, y)$ 能够取得最大长度 $k,ドル我们知道 $k$ 取决于以点 $(x, y)$ 为起点,四联通方向中「最短的连续 1ドル$ 的长度」。
45+
46+
基于此,我们可以建立四个大小为 $n \times n$ 的矩阵(二维数组)`a``b``c``d` 分别代表四个方向连续 1ドル$ 的前缀数:
47+
48+
![image.png](https://pic.leetcode.cn/1667958744-jszheo-image.png)
49+
50+
数据范围为 500ドル,ドル预处理前缀数组复杂度为 $O(n^2),ドル统计答案复杂度为 $O(n^2),ドル时间复杂度没有问题。
51+
52+
再考虑空间,建立四个方向的前缀数组所需空间为 500ドル \times 500 \times 4 = 10^6,ドル即使加上原矩阵 `g` 也不会有 `MLE` 风险,空间复杂度也没有问题。
53+
54+
Java 代码:
55+
```Java
56+
class Solution {
57+
public int orderOfLargestPlusSign(int n, int[][] mines) {
58+
int[][] g = new int[n + 10][n + 10];
59+
for (int i = 1; i <= n; i++) Arrays.fill(g[i], 1);
60+
for (int[] info : mines) g[info[0] + 1][info[1] + 1] = 0;
61+
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];
62+
for (int i = 1; i <= n; i++) {
63+
for (int j = 1; j <= n; j++) {
64+
if (g[i][j] == 1) {
65+
a[i][j] = a[i - 1][j] + 1;
66+
b[i][j] = b[i][j - 1] + 1;
67+
}
68+
if (g[n + 1 - i][n + 1 - j] == 1) {
69+
c[n + 1 - i][n + 1 - j] = c[n + 2 - i][n + 1 - j] + 1;
70+
d[n + 1 - i][n + 1 - j] = d[n + 1 - i][n + 2 - j] + 1;
71+
}
72+
}
73+
}
74+
int ans = 0;
75+
for (int i = 1; i <= n; i++) {
76+
for (int j = 1; j <= n; j++) {
77+
ans = Math.max(ans, Math.min(Math.min(a[i][j], b[i][j]), Math.min(c[i][j], d[i][j])));
78+
}
79+
}
80+
return ans;
81+
}
82+
}
83+
```
84+
TypeScript 代码:
85+
```TypeScript
86+
function orderOfLargestPlusSign(n: number, mines: number[][]): number {
87+
function getMat(x: number, y: number, val: number): number[][] {
88+
const ans = new Array<Array<number>>(x)
89+
for (let i = 0; i < x; i++) ans[i] = new Array<number>(y).fill(val)
90+
return ans
91+
}
92+
const g = getMat(n + 10, n + 10, 1)
93+
for (const info of mines) g[info[0] + 1][info[1] + 1] = 0
94+
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)
95+
for (let i = 1; i <= n; i++) {
96+
for (let j = 1; j <= n; j++) {
97+
if (g[i][j] == 1) {
98+
a[i][j] = a[i - 1][j] + 1
99+
b[i][j] = b[i][j - 1] + 1
100+
}
101+
if (g[n + 1 - i][n + 1 - j] == 1) {
102+
c[n + 1 - i][n + 1 - j] = c[n + 2 - i][n + 1 - j] + 1
103+
d[n + 1 - i][n + 1 - j] = d[n + 1 - i][n + 2 - j] + 1
104+
}
105+
}
106+
}
107+
let ans = 0
108+
for (let i = 1; i <= n; i++) {
109+
for (let j = 1; j <= n; j++) {
110+
ans = Math.max(ans, Math.min(Math.min(a[i][j], b[i][j]), Math.min(c[i][j], d[i][j])))
111+
}
112+
}
113+
return ans
114+
}
115+
```
116+
Python 代码:
117+
```Python
118+
class Solution:
119+
def orderOfLargestPlusSign(self, n: int, mines: List[List[int]]) -> int:
120+
g = [[1] * (n + 10) for _ in range(n + 10)]
121+
for x, y in mines:
122+
g[x + 1][y + 1] = 0
123+
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)]
124+
for i in range(1, n + 1):
125+
for j in range(1, n + 1):
126+
if g[i][j] == 1:
127+
a[i][j] = a[i - 1][j] + 1
128+
b[i][j] = b[i][j - 1] + 1
129+
if g[n + 1 - i][n + 1 - j] == 1:
130+
c[n + 1 - i][n + 1 - j] = c[n + 2 - i][n + 1 - j] + 1
131+
d[n + 1 - i][n + 1 - j] = d[n + 1 - i][n + 2 - j] + 1
132+
ans = 0
133+
for i in range(1, n + 1):
134+
for j in range(1, n + 1):
135+
ans = max(ans, min(min(a[i][j], b[i][j]), min(c[i][j], d[i][j])))
136+
return ans
137+
```
138+
* 时间复杂度:$O(n^2)$
139+
* 空间复杂度:$O(n^2)$
140+
141+
---
142+
143+
### 最后
144+
145+
这是我们「刷穿 LeetCode」系列文章的第 `No.764` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
146+
147+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
148+
149+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
150+
151+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
152+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /