Skip to content

Commit 743df8d

Browse files
committed
补充第 1200 ~ 1299 题的题目解析(增加 38 道题)
1 parent 8d78c18 commit 743df8d

40 files changed

Lines changed: 5018 additions & 1 deletion

‎docs/00_preface/00_05_solutions_list.md‎

Lines changed: 39 additions & 1 deletion
Large diffs are not rendered by default.

‎docs/solutions/1200-1299/index.md‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,87 @@
11
## 本章内容
22

3+
- [1200. 最小绝对差](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/minimum-absolute-difference.md)
4+
- [1201. 丑数 III](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/ugly-number-iii.md)
35
- [1202. 交换字符串中的元素](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/smallest-string-with-swaps.md)
6+
- [1203. 项目管理](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/sort-items-by-groups-respecting-dependencies.md)
47
- [1206. 设计跳表](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/design-skiplist.md)
8+
- [1207. 独一无二的出现次数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/unique-number-of-occurrences.md)
59
- [1208. 尽可能使字符串相等](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/get-equal-substrings-within-budget.md)
10+
- [1209. 删除字符串中的所有相邻重复项 II](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/remove-all-adjacent-duplicates-in-string-ii.md)
11+
- [1210. 穿过迷宫的最少移动次数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/minimum-moves-to-reach-target-with-rotations.md)
612
- [1213. 三个有序数组的交集](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/intersection-of-three-sorted-arrays.md)
13+
- [1214. 查找两棵二叉搜索树之和](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/two-sum-bsts.md)
14+
- [1215. 步进数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/stepping-numbers.md)
15+
- [1216. 验证回文串 III](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/valid-palindrome-iii.md)
716
- [1217. 玩筹码](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/minimum-cost-to-move-chips-to-the-same-position.md)
817
- [1218. 最长定差子序列](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/longest-arithmetic-subsequence-of-given-difference.md)
18+
- [1219. 黄金矿工](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/path-with-maximum-gold.md)
919
- [1220. 统计元音字母序列的数目](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/count-vowels-permutation.md)
20+
- [1221. 分割平衡字符串](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/split-a-string-in-balanced-strings.md)
21+
- [1222. 可以攻击国王的皇后](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/queens-that-can-attack-the-king.md)
1022
- [1223. 掷骰子模拟](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/dice-roll-simulation.md)
1123
- [1224. 最大相等频率](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/maximum-equal-frequency.md)
24+
- [1226. 哲学家进餐](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/the-dining-philosophers.md)
1225
- [1227. 飞机座位分配概率](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/airplane-seat-assignment-probability.md)
26+
- [1228. 等差数列中缺失的数字](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/missing-number-in-arithmetic-progression.md)
1327
- [1229. 安排会议日程](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/meeting-scheduler.md)
28+
- [1230. 抛掷硬币](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/toss-strange-coins.md)
1429
- [1231. 分享巧克力](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/divide-chocolate.md)
1530
- [1232. 缀点成线](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/check-if-it-is-a-straight-line.md)
31+
- [1233. 删除子文件夹](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/remove-sub-folders-from-the-filesystem.md)
32+
- [1234. 替换子串得到平衡字符串](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/replace-the-substring-for-balanced-string.md)
1633
- [1235. 规划兼职工作](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/maximum-profit-in-job-scheduling.md)
34+
- [1236. 网络爬虫](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/web-crawler.md)
1735
- [1237. 找出给定方程的正整数解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/find-positive-integer-solution-for-a-given-equation.md)
1836
- [1238. 循环码排列](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/circular-permutation-in-binary-representation.md)
1937
- [1239. 串联字符串的最大长度](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/maximum-length-of-a-concatenated-string-with-unique-characters.md)
38+
- [1240. 铺瓷砖](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/tiling-a-rectangle-with-the-fewest-squares.md)
2039
- [1243. 数组变换](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/array-transformation.md)
2140
- [1244. 力扣排行榜](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/design-a-leaderboard.md)
2241
- [1245. 树的直径](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/tree-diameter.md)
42+
- [1246. 删除回文子数组](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/palindrome-removal.md)
2343
- [1247. 交换字符使得字符串相同](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/minimum-swaps-to-make-strings-equal.md)
2444
- [1248. 统计「优美子数组」](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/count-number-of-nice-subarrays.md)
45+
- [1249. 移除无效的括号](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/minimum-remove-to-make-valid-parentheses.md)
2546
- [1250. 检查「好数组」](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/check-if-it-is-a-good-array.md)
2647
- [1252. 奇数值单元格的数目](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/cells-with-odd-values-in-a-matrix.md)
2748
- [1253. 重构 2 行二进制矩阵](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/reconstruct-a-2-row-binary-matrix.md)
2849
- [1254. 统计封闭岛屿的数目](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/number-of-closed-islands.md)
2950
- [1255. 得分最高的单词集合](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/maximum-score-words-formed-by-letters.md)
3051
- [1256. 加密数字](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/encode-number.md)
52+
- [1257. 最小公共区域](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/smallest-common-region.md)
53+
- [1258. 近义词句子](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/synonymous-sentences.md)
3154
- [1259. 不相交的握手](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/handshakes-that-dont-cross.md)
55+
- [1260. 二维网格迁移](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/shift-2d-grid.md)
3256
- [1261. 在受污染的二叉树中查找元素](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/find-elements-in-a-contaminated-binary-tree.md)
3357
- [1262. 可被三整除的最大和](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/greatest-sum-divisible-by-three.md)
58+
- [1263. 推箱子](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/minimum-moves-to-move-a-box-to-their-target-location.md)
59+
- [1265. 逆序打印不可变链表](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/print-immutable-linked-list-in-reverse.md)
3460
- [1266. 访问所有点的最小时间](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/minimum-time-visiting-all-points.md)
3561
- [1267. 统计参与通信的服务器](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/count-servers-that-communicate.md)
3662
- [1268. 搜索推荐系统](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/search-suggestions-system.md)
63+
- [1269. 停在原地的方案数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/number-of-ways-to-stay-in-the-same-place-after-some-steps.md)
3764
- [1271. 十六进制魔术数字](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/hexspeak.md)
65+
- [1272. 删除区间](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/remove-interval.md)
3866
- [1273. 删除树节点](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/delete-tree-nodes.md)
67+
- [1274. 矩形内船只的数目](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/number-of-ships-in-a-rectangle.md)
3968
- [1275. 找出井字棋的获胜者](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/find-winner-on-a-tic-tac-toe-game.md)
69+
- [1276. 不浪费原料的汉堡制作方案](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/number-of-burgers-with-no-waste-of-ingredients.md)
4070
- [1277. 统计全为 1 的正方形子矩阵](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/count-square-submatrices-with-all-ones.md)
71+
- [1278. 分割回文串 III](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/palindrome-partitioning-iii.md)
4172
- [1281. 整数的各位积和之差](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/subtract-the-product-and-sum-of-digits-of-an-integer.md)
4273
- [1282. 用户分组](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/group-the-people-given-the-group-size-they-belong-to.md)
4374
- [1283. 使结果不超过阈值的最小除数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/find-the-smallest-divisor-given-a-threshold.md)
75+
- [1284. 转化为全零矩阵的最少反转次数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.md)
4476
- [1286. 字母组合迭代器](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/iterator-for-combination.md)
4577
- [1287. 有序数组中出现次数超过25%的元素](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/element-appearing-more-than-25-in-sorted-array.md)
78+
- [1288. 删除被覆盖区间](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/remove-covered-intervals.md)
79+
- [1289. 下降路径最小和 II](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/minimum-falling-path-sum-ii.md)
4680
- [1290. 二进制链表转整数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/convert-binary-number-in-a-linked-list-to-integer.md)
81+
- [1291. 顺次数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/sequential-digits.md)
82+
- [1292. 元素和小于等于阈值的正方形的最大边长](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold.md)
83+
- [1293. 网格中的最短路径](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/shortest-path-in-a-grid-with-obstacles-elimination.md)
4784
- [1295. 统计位数为偶数的数字](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/find-numbers-with-even-number-of-digits.md)
4885
- [1296. 划分数组为连续数字的集合](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/divide-array-in-sets-of-k-consecutive-numbers.md)
4986
- [1297. 子串的最大出现次数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/maximum-number-of-occurrences-of-a-substring.md)
87+
- [1299. 将每个元素替换为右侧最大元素](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/1200-1299/replace-elements-with-greatest-element-on-right-side.md)
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# [1292. 元素和小于等于阈值的正方形的最大边长](https://leetcode.cn/problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold/)
2+
3+
- 标签:数组、二分查找、矩阵、前缀和
4+
- 难度:中等
5+
6+
## 题目链接
7+
8+
- [1292. 元素和小于等于阈值的正方形的最大边长 - 力扣](https://leetcode.cn/problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold/)
9+
10+
## 题目大意
11+
12+
**描述**:给定一个 $m \times n$ 的矩阵 $mat$ 和一个整数阈值 $threshold$。
13+
14+
**要求**:返回矩阵中元素和不超过 $threshold$ 的正方形的最大边长。如果没有这样的正方形,返回 $0$。
15+
16+
**说明**:
17+
18+
- $1 \le m, n \le 300$。
19+
- $0 \le mat[i][j] \le 10^{5}$。
20+
- $0 \le threshold \le 10^{9}$。
21+
22+
**示例**:
23+
24+
- 示例 1:
25+
26+
```python
27+
输入:mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4
28+
输出:2
29+
解释:边长为 2 的正方形有 4 个,元素和分别为 4、4、4、9,最大边长为 2。
30+
```
31+
32+
- 示例 2:
33+
34+
```python
35+
输入:mat = [[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2]], threshold = 1
36+
输出:0
37+
```
38+
39+
## 解题思路
40+
41+
### 思路 1:二维前缀和 + 枚举边长
42+
43+
#### 1. 核心思想
44+
45+
要快速计算任意正方形的元素和,需要用到**二维前缀和**。二维前缀和 $prefix[i][j]$ 表示矩阵从左上角 $(0,0)$ 到 $(i-1,j-1)$ 这个矩形区域内所有元素的和。通过容斥原理,可以在 $O(1)$ 时间内计算出任意矩形区域的和。
46+
47+
有了二维前缀和后,可以直接枚举所有可能的正方形(左上角位置和边长),检查其元素和是否 $\le threshold$,取最大边长。
48+
49+
总时间复杂度 $O(m \times n \times \min(m, n))$。$m, n \le 300$,最坏 $300 \times 300 \times 300 = 2700$ 万,勉强可行。但可以用二分查找优化到 $O(mn \times \log(\min(m,n)))$。
50+
51+
#### 2. 二维前缀和公式
52+
53+
$prefix[i+1][j+1] = prefix[i][j+1] + prefix[i+1][j] - prefix[i][j] + mat[i][j]$
54+
55+
$(r_1, c_1)$ 到 $(r_2, c_2)$ 的矩形和(左上角为 $(r_1, c_1)$,右下角为 $(r_2, c_2)$):
56+
57+
$sum = prefix[r_2+1][c_2+1] - prefix[r_1][c_2+1] - prefix[r_2+1][c_1] + prefix[r_1][c_1]$
58+
59+
#### 3. 具体步骤
60+
61+
**第 1 步**:构建二维前缀和数组 $prefix$,大小为 $(m+1) \times (n+1)$。
62+
63+
**第 2 步**:枚举边长 $k$(从 $\min(m, n)$ 到 $1$),检查是否存在一个 $k \times k$ 的正方形满足条件:
64+
- 遍历所有左上角位置 $(i, j)$,其中 $0 \le i \le m-k$,$0 \le j \le n-k$。
65+
- 用前缀和计算正方形元素和,如果 $\le threshold$,返回 $k$。
66+
67+
**第 3 步**:如果没有找到,返回 $0$。
68+
69+
### 思路 1:代码
70+
71+
```python
72+
class Solution:
73+
def maxSideLength(self, mat: List[List[int]], threshold: int) -> int:
74+
m, n = len(mat), len(mat[0])
75+
# 构建二维前缀和
76+
prefix = [[0] * (n + 1) for _ in range(m + 1)]
77+
for i in range(m):
78+
for j in range(n):
79+
prefix[i + 1][j + 1] = prefix[i][j + 1] + prefix[i + 1][j] - prefix[i][j] + mat[i][j]
80+
81+
# 从大到小枚举边长
82+
max_side = min(m, n)
83+
for k in range(max_side, 0, -1):
84+
for i in range(m - k + 1):
85+
for j in range(n - k + 1):
86+
# 计算以 (i,j) 为左上角,边长为 k 的正方形的元素和
87+
r1, c1 = i, j
88+
r2, c2 = i + k - 1, j + k - 1
89+
total = (prefix[r2 + 1][c2 + 1] - prefix[r1][c2 + 1]
90+
- prefix[r2 + 1][c1] + prefix[r1][c1])
91+
if total <= threshold:
92+
return k
93+
return 0
94+
```
95+
96+
### 思路 1:复杂度分析
97+
98+
- **时间复杂度**:$O(mn \times \min(m, n))$,二维前缀和构建 $O(mn)$,枚举正方形最坏 $O(mn \times \min(m,n))$。
99+
- **空间复杂度**:$O(mn)$,存储二维前缀和数组。
100+
101+
### 思路 2:二维前缀和 + 二分答案
102+
103+
#### 1. 核心思想
104+
105+
边长的合法性具有单调性:如果边长为 $k$ 存在满足条件的正方形,那么边长 $< k$ 也一定存在(因为更小的正方形和更小)。反之,如果边长为 $k$ 不存在,那么边长 $> k$ 也不存在。
106+
107+
因此可以对边长进行二分查找,将时间复杂度降到 $O(mn \times \log(\min(m,n)))$。
108+
109+
#### 2. 具体步骤
110+
111+
**第 1 步**:构建二维前缀和数组。
112+
113+
**第 2 步**:在 $[0, \min(m, n)]$ 范围内二分查找最大边长:
114+
- $check(k)$:检查是否存在一个 $k \times k$ 的正方形,其元素和 $\le threshold$。
115+
- 如果 $check(mid)$ 为真,说明边长还可以更大,$left = mid$。
116+
- 否则 $right = mid - 1$。
117+
118+
**第 3 步**:返回 $left$。
119+
120+
#### 3. 代码
121+
122+
```python
123+
class Solution:
124+
def maxSideLength(self, mat: List[List[int]], threshold: int) -> int:
125+
m, n = len(mat), len(mat[0])
126+
# 构建二维前缀和
127+
prefix = [[0] * (n + 1) for _ in range(m + 1)]
128+
for i in range(m):
129+
for j in range(n):
130+
prefix[i + 1][j + 1] = prefix[i][j + 1] + prefix[i + 1][j] - prefix[i][j] + mat[i][j]
131+
132+
# 检查是否存在边长为 k 的满足条件的正方形
133+
def can(k: int) -> bool:
134+
for i in range(m - k + 1):
135+
for j in range(n - k + 1):
136+
total = (prefix[i + k][j + k] - prefix[i][j + k]
137+
- prefix[i + k][j] + prefix[i][j])
138+
if total <= threshold:
139+
return True
140+
return False
141+
142+
# 二分查找最大边长
143+
left, right = 0, min(m, n)
144+
while left < right:
145+
mid = (left + right + 1) // 2
146+
if can(mid):
147+
left = mid
148+
else:
149+
right = mid - 1
150+
return left
151+
```
152+
153+
#### 4. 复杂度分析
154+
155+
- **时间复杂度**:$O(mn \times \log(\min(m, n)))$,二分需要 $O(\log(\min(m,n)))$ 次,每次 $check$ 需要 $O(mn)$ 时间。
156+
- **空间复杂度**:$O(mn)$。
157+
158+
对于 $m, n \le 300$,思路 1 已经足够快。但如果矩阵更大(如 $1000 \times 1000$),思路 2 的优势就会明显体现出来。

0 commit comments

Comments
 (0)