Skip to content

Commit e7ed231

Browse files
authored
update: 添加问题“1292.元素和小于等于阈值的正方形的最大边长”的代码和题解 (#1335)
* en: 2026.1.19 * 1292: AC.cpp (#1334) - AC,91.34%,58.44% 一遍过了 * update: 添加问题“1292.元素和小于等于阈值的正方形的最大边长”的代码和题解 (#1335) Signed-off-by: LetMeFly666 <Tisfy@qq.com> --------- Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent 157205f commit e7ed231

11 files changed

Lines changed: 154 additions & 79 deletions

.commitTitleExtra

Lines changed: 0 additions & 1 deletion
This file was deleted.

.commitmsg

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-01-19 21:47:34
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-01-19 21:55:16
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int maxSideLength(vector<vector<int>>& mat, int threshold) {
14+
int n = mat.size(), m = mat[0].size();
15+
vector<vector<int>> prefix(n + 1, vector<int>(m + 1));
16+
for (int i = 0; i < n; i++) {
17+
for (int j = 0; j < m; j++) {
18+
prefix[i + 1][j + 1] = mat[i][j] - prefix[i][j] + prefix[i][j + 1] + prefix[i + 1][j];
19+
}
20+
}
21+
22+
int ans = 0;
23+
for (int i = 0; i < n; i++) {
24+
for (int j = 0; j < m; j++) {
25+
while (i + ans < n && j + ans < m && prefix[i + ans + 1][j + ans + 1] - prefix[i + ans + 1][j] - prefix[i][j + ans + 1] + prefix[i][j] <= threshold) {
26+
ans++;
27+
}
28+
}
29+
}
30+
return ans;
31+
}
32+
};

Codes/1895-largest-magic-square.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
* @Author: LetMeFly
33
* @Date: 2026-01-18 20:47:19
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2026-01-18 23:16:07
5+
* @LastEditTime: 2026-01-18 23:17:50
66
*/
77
#if defined(_WIN32) || defined(__APPLE__)
88
#include "_[1,2]toVector.h"
99
#endif
1010

11-
// THIS CANNOT BE ACCEPTED
1211
class Solution {
1312
private:
1413
vector<vector<int>> rowSum, colSum;
@@ -17,7 +16,7 @@ class Solution {
1716
int cnt = 0, cntRev = 0;
1817
for (int i = 0; i < l; i++) {
1918
cnt += grid[x + i][y + i];
20-
cntRev += grid[x + l - i - 1][y + l - i - 1];
19+
cntRev += grid[x + i][y + l - i - 1]; // 易错
2120
}
2221
if (cnt != cntRev) {
2322
return false;

Codes/1895-largest-magic-square_AC.cpp

Lines changed: 0 additions & 58 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@
542542
|1287.有序数组中出现次数超过25%的元素|简单|<a href="https://leetcode.cn/problems/element-appearing-more-than-25-in-sorted-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/02/17/LeetCode%201287.%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E5%87%BA%E7%8E%B0%E6%AC%A1%E6%95%B0%E8%B6%85%E8%BF%8725%E7%9A%84%E5%85%83%E7%B4%A0" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145683090" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/element-appearing-more-than-25-in-sorted-array/solutions/3078136/letmefly-1287you-xu-shu-zu-zhong-chu-xia-wdat/" target="_blank">LeetCode题解</a>|
543543
|1289.下降路径最小和II|困难|<a href="https://leetcode.cn/problems/minimum-falling-path-sum-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/08/10/LeetCode%201289.%E4%B8%8B%E9%99%8D%E8%B7%AF%E5%BE%84%E6%9C%80%E5%B0%8F%E5%92%8CII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/132201281" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-falling-path-sum-ii/solutions/2381173/letmefly-1289xia-jiang-lu-jing-zui-xiao-kt8cj/" target="_blank">LeetCode题解</a>|
544544
|1290.二进制链表转整数|简单|<a href="https://leetcode.cn/problems/convert-binary-number-in-a-linked-list-to-integer/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/07/14/LeetCode%201290.%E4%BA%8C%E8%BF%9B%E5%88%B6%E9%93%BE%E8%A1%A8%E8%BD%AC%E6%95%B4%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/149342077" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/convert-binary-number-in-a-linked-list-to-integer/solutions/3723954/letmefly-1290er-jin-zhi-lian-biao-zhuan-adolg/" target="_blank">LeetCode题解</a>|
545+
|1292.元素和小于等于阈值的正方形的最大边长|中等|<a href="https://leetcode.cn/problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/01/19/LeetCode%201292.%E5%85%83%E7%B4%A0%E5%92%8C%E5%B0%8F%E4%BA%8E%E7%AD%89%E4%BA%8E%E9%98%88%E5%80%BC%E7%9A%84%E6%AD%A3%E6%96%B9%E5%BD%A2%E7%9A%84%E6%9C%80%E5%A4%A7%E8%BE%B9%E9%95%BF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/157145833" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold/solutions/3884572/letmefly-1292yuan-su-he-xiao-yu-deng-yu-4220r/" target="_blank">LeetCode题解</a>|
545546
|1295.统计位数为偶数的数字|简单|<a href="https://leetcode.cn/problems/find-numbers-with-even-number-of-digits/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/30/LeetCode%201295.%E7%BB%9F%E8%AE%A1%E4%BD%8D%E6%95%B0%E4%B8%BA%E5%81%B6%E6%95%B0%E7%9A%84%E6%95%B0%E5%AD%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147637587" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-numbers-with-even-number-of-digits/solutions/3666390/letmefly-1295tong-ji-wei-shu-wei-ou-shu-bwhfr/" target="_blank">LeetCode题解</a>|
546547
|1299.将每个元素替换为右侧最大元素|简单|<a href="https://leetcode.cn/problems/replace-elements-with-greatest-element-on-right-side/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/02/16/LeetCode%201299.%E5%B0%86%E6%AF%8F%E4%B8%AA%E5%85%83%E7%B4%A0%E6%9B%BF%E6%8D%A2%E4%B8%BA%E5%8F%B3%E4%BE%A7%E6%9C%80%E5%A4%A7%E5%85%83%E7%B4%A0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145661909" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/replace-elements-with-greatest-element-on-right-side/solutions/3076497/letmefly-1299jiang-mei-ge-yuan-su-ti-hua-4y58/" target="_blank">LeetCode题解</a>|
547548
|1302.层数最深叶子节点的和|中等|<a href="https://leetcode.cn/problems/deepest-leaves-sum/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/17/LeetCode%201302.%E5%B1%82%E6%95%B0%E6%9C%80%E6%B7%B1%E5%8F%B6%E5%AD%90%E8%8A%82%E7%82%B9%E7%9A%84%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126377912" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/deepest-leaves-sum/solution/letmefly-1302ceng-shu-zui-shen-xie-zi-ji-eyu0/" target="_blank">LeetCode题解</a>|
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
title: 1292.元素和小于等于阈值的正方形的最大边长:二维前缀和(无需二分)+抽象速懂的描述
3+
date: 2026-01-19 23:27:08
4+
tags: [题解, LeetCode, 中等, 数组, 矩阵, 前缀和]
5+
categories: [题解, LeetCode]
6+
index_img: https://assets.leetcode.cn/aliyun-lc-upload/uploads/2019/12/15/e1.png
7+
---
8+
9+
# 【LetMeFly】1292.元素和小于等于阈值的正方形的最大边长:二维前缀和(无需二分)+抽象速懂的描述
10+
11+
力扣题目链接:[https://leetcode.cn/problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold/](https://leetcode.cn/problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold/)
12+
13+
<p>给你一个大小为&nbsp;<code>m x n</code>&nbsp;的矩阵&nbsp;<code>mat</code>&nbsp;和一个整数阈值&nbsp;<code>threshold</code>。</p>
14+
15+
<p>请你返回元素总和小于或等于阈值的正方形区域的最大边长;如果没有这样的正方形区域,则返回 <strong>0&nbsp;</strong>。<br />
16+
&nbsp;</p>
17+
18+
<p><strong>示例 1:</strong></p>
19+
20+
<p><img alt="" src="https://assets.leetcode.cn/aliyun-lc-upload/uploads/2019/12/15/e1.png" style="height: 186px; width: 335px;" /></p>
21+
22+
<pre>
23+
<strong>输入:</strong>mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4
24+
<strong>输出:</strong>2
25+
<strong>解释:</strong>总和小于或等于 4 的正方形的最大边长为 2,如图所示。
26+
</pre>
27+
28+
<p><strong>示例 2:</strong></p>
29+
30+
<pre>
31+
<strong>输入:</strong>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
32+
<strong>输出:</strong>0
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
37+
<p><strong>提示:</strong></p>
38+
39+
<ul>
40+
<li><code>m == mat.length</code></li>
41+
<li><code>n == mat[i].length</code></li>
42+
<li><code>1 &lt;= m, n &lt;= 300</code></li>
43+
<li><code>0 &lt;= mat[i][j] &lt;= 10<sup>4</sup></code></li>
44+
<li><code>0 &lt;= threshold &lt;= 10<sup>5</sup></code><sup>&nbsp;</sup></li>
45+
</ul>
46+
47+
48+
49+
## 解题方法:前缀和
50+
51+
二维矩阵的二维前缀和可以快速计算出某个子矩阵的元素和。
52+
53+
```
54+
AB
55+
CD
56+
```
57+
58+
其中`prefix[D]`代表从左上角到`D`这个矩阵的元素和,计算方法为`D+B+C-A`
59+
60+
```
61+
ABC
62+
DEF
63+
GHI
64+
```
65+
66+
那么想计算`EFHI`这个子矩阵的元素和就只需要`prefix[I]-prefix[C]-prefix[G]+prefix[A]`
67+
68+
二层循环枚举矩阵左上角顶点,使用一个变量`ans`作为答案合法边长并且**只增不减**,那么二层循环时间复杂度$O(mn)$,内层`ans`总时间复杂度不会超过$O\min(m,n)$。
69+
70+
+ 时间复杂度$O(mn)$
71+
+ 空间复杂度$O(N\log N)$
72+
73+
### AC代码
74+
75+
#### C++
76+
77+
```cpp
78+
/*
79+
* @LastEditTime: 2026-01-19 21:55:16
80+
*/
81+
class Solution {
82+
public:
83+
int maxSideLength(vector<vector<int>>& mat, int threshold) {
84+
int n = mat.size(), m = mat[0].size();
85+
vector<vector<int>> prefix(n + 1, vector<int>(m + 1));
86+
for (int i = 0; i < n; i++) {
87+
for (int j = 0; j < m; j++) {
88+
prefix[i + 1][j + 1] = mat[i][j] - prefix[i][j] + prefix[i][j + 1] + prefix[i + 1][j];
89+
}
90+
}
91+
92+
int ans = 0;
93+
for (int i = 0; i < n; i++) {
94+
for (int j = 0; j < m; j++) {
95+
while (i + ans < n && j + ans < m && prefix[i + ans + 1][j + ans + 1] - prefix[i + ans + 1][j] - prefix[i][j + ans + 1] + prefix[i][j] <= threshold) {
96+
ans++;
97+
}
98+
}
99+
}
100+
return ans;
101+
}
102+
};
103+
```
104+
105+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/157145833)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/01/19/LeetCode%201292.%E5%85%83%E7%B4%A0%E5%92%8C%E5%B0%8F%E4%BA%8E%E7%AD%89%E4%BA%8E%E9%98%88%E5%80%BC%E7%9A%84%E6%AD%A3%E6%96%B9%E5%BD%A2%E7%9A%84%E6%9C%80%E5%A4%A7%E8%BE%B9%E9%95%BF/)~
106+
>
107+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-Accumulation-SomeTips.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ Interpreter: /bin/bash
385385

386386
### zsh里面 path和PATH是一个
387387

388-
ZSH中修改$path$变量也会自动修改$PATH$变量。
388+
ZSH中修改`$path`变量也会自动修改`$PATH`变量。
389389

390390
### Linux登录欢迎语motd
391391

@@ -648,7 +648,7 @@ f35b2f66-3e03-4c9d-80b5-d72059a8735d
648648
1. 固件支持WOL
649649
2. 局域网或公网可达
650650
3. 网卡有待机电
651-
4. 接收到魔术网络包(Magic Packe),内容`6 字节的 FF`+`目标网卡 MAC 地址 × 16 次`
651+
4. 接收到魔术网络包(Magic Packet),内容`6 字节的 FF`+`目标网卡 MAC 地址 × 16 次`
652652

653653
没有认证,知道Mac地址就能发。
654654

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,9 @@ categories: [自用]
17461746
|||
17471747
|bureau|n. 局,处,科|
17481748
|disintegration|n. 崩解,瓦解,裂变|
1749+
|||
1750+
|reconnaissance|n. 侦察|
1751+
|illegible|adj. 难以辨认的,无法辨识的,字迹模糊的|
17491752

17501753
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
17511754
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)

toSay.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!--
2+
* @Author: LetMeFly
3+
* @Date: 2026-01-18 23:44:01
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-01-19 21:45:08
6+
-->
7+
曝腾讯向GitHub发函,要求下架“用户可获取自己聊天记录”的仓库

0 commit comments

Comments
 (0)