Skip to content

Commit 157205f

Browse files
committed
update: 添加问题“1895.最大的幻方”的代码和题解+ 日积月累(WOL|ZSH path+PATH) (#1333)
1895: AC.cpp(#1332) cpp - AC,21.28%,60.64% --- 上次的commit msg应该是: 9ddd607 3047: WA.cpp+AC.cpp (#1329) + word(en) cpp - AC,5.45%,16.36% cpp(diff2) - AC,5.45%,47.27% en: 2026.1.16 + 2026.1.17 Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent 9ddd607 commit 157205f

8 files changed

Lines changed: 275 additions & 2 deletions

.commitTitleExtra

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+ 日积月累(WOL|ZSH path+PATH)

.commitmsg

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1-
2942: AC.cpp(#1326) - AC,100.00%,84.62% + en
1+
1895: AC.cpp(#1332)
22

3-
docs: en(2026.1.14+)2026.1.15
3+
cpp - AC,21.28%,60.64%
4+
5+
---
6+
上次的commit msg应该是:
7+
8+
https://github.com/LetMeFly666/LeetCode/commit/9ddd607074f67013efc04fc5c6b8f82883ae5786
9+
10+
3047: WA.cpp+AC.cpp (#1329) + word(en)
11+
12+
cpp - AC,5.45%,16.36%
13+
cpp(diff2) - AC,5.45%,47.27%
14+
en: 2026.1.16 + 2026.1.17
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-01-18 20:47:19
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-01-18 23:16:07
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
// THIS CANNOT BE ACCEPTED
12+
class Solution {
13+
private:
14+
vector<vector<int>> rowSum, colSum;
15+
16+
bool ok(vector<vector<int>>& grid, int x, int y, int l) {
17+
int cnt = 0, cntRev = 0;
18+
for (int i = 0; i < l; i++) {
19+
cnt += grid[x + i][y + i];
20+
cntRev += grid[x + l - i - 1][y + l - i - 1];
21+
}
22+
if (cnt != cntRev) {
23+
return false;
24+
}
25+
for (int i = 0; i < l; i++) {
26+
if (rowSum[x + i][y + l] - rowSum[x + i][y] != cnt) {
27+
return false;
28+
}
29+
if (colSum[x + l][y + i] - colSum[x][y + i] != cnt) {
30+
return false;
31+
}
32+
}
33+
return true;
34+
}
35+
public:
36+
int largestMagicSquare(vector<vector<int>>& grid) {
37+
int n = grid.size(), m = grid[0].size();
38+
rowSum.resize(n, vector<int>(m + 1));
39+
colSum.resize(n + 1, vector<int>(m));
40+
41+
for (int i = 0; i < n; i++) {
42+
for (int j = 0; j < m; j++) {
43+
rowSum[i][j + 1] = rowSum[i][j] + grid[i][j];
44+
colSum[i + 1][j] = colSum[i][j] + grid[i][j];
45+
}
46+
}
47+
48+
for (int k = n; k > 1; k--) {
49+
for (int i = 0; i + k <= n; i++) {
50+
for (int j = 0; j + k <= m; j++) {
51+
if (ok(grid, i, j, k)) {
52+
return k;
53+
}
54+
}
55+
}
56+
}
57+
return 1;
58+
}
59+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-01-18 20:47:19
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-01-18 23:17:50
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
vector<vector<int>> rowSum, colSum;
14+
15+
bool ok(vector<vector<int>>& grid, int x, int y, int l) {
16+
int cnt = 0, cntRev = 0;
17+
for (int i = 0; i < l; i++) {
18+
cnt += grid[x + i][y + i];
19+
cntRev += grid[x + i][y + l - i - 1]; // 易错
20+
}
21+
if (cnt != cntRev) {
22+
return false;
23+
}
24+
for (int i = 0; i < l; i++) {
25+
if (rowSum[x + i][y + l] - rowSum[x + i][y] != cnt) {
26+
return false;
27+
}
28+
if (colSum[x + l][y + i] - colSum[x][y + i] != cnt) {
29+
return false;
30+
}
31+
}
32+
return true;
33+
}
34+
public:
35+
int largestMagicSquare(vector<vector<int>>& grid) {
36+
int n = grid.size(), m = grid[0].size();
37+
rowSum.resize(n, vector<int>(m + 1));
38+
colSum.resize(n + 1, vector<int>(m));
39+
40+
for (int i = 0; i < n; i++) {
41+
for (int j = 0; j < m; j++) {
42+
rowSum[i][j + 1] = rowSum[i][j] + grid[i][j];
43+
colSum[i + 1][j] = colSum[i][j] + grid[i][j];
44+
}
45+
}
46+
47+
for (int k = n; k > 1; k--) {
48+
for (int i = 0; i + k <= n; i++) {
49+
for (int j = 0; j + k <= m; j++) {
50+
if (ok(grid, i, j, k)) {
51+
return k;
52+
}
53+
}
54+
}
55+
}
56+
return 1;
57+
}
58+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@
678678
|1863.找出所有子集的异或总和再求和|简单|<a href="https://leetcode.cn/problems/sum-of-all-subset-xor-totals/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/06/LeetCode%201863.%E6%89%BE%E5%87%BA%E6%89%80%E6%9C%89%E5%AD%90%E9%9B%86%E7%9A%84%E5%BC%82%E6%88%96%E6%80%BB%E5%92%8C%E5%86%8D%E6%B1%82%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147027120" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/sum-of-all-subset-xor-totals/solutions/3642017/letmefly-1863zhao-chu-suo-you-zi-ji-de-y-a02c/" target="_blank">LeetCode题解</a>|
679679
|1870.准时到达的列车最小时速|中等|<a href="https://leetcode.cn/problems/minimum-speed-to-arrive-on-time/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/02/LeetCode%201870.%E5%87%86%E6%97%B6%E5%88%B0%E8%BE%BE%E7%9A%84%E5%88%97%E8%BD%A6%E6%9C%80%E5%B0%8F%E6%97%B6%E9%80%9F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142680612" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-speed-to-arrive-on-time/solutions/2937033/letmefly-1870zhun-shi-dao-da-de-lie-che-liv8g/" target="_blank">LeetCode题解</a>|
680680
|1884.鸡蛋掉落-两枚鸡蛋|中等|<a href="https://leetcode.cn/problems/egg-drop-with-2-eggs-and-n-floors/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/13/LeetCode%201884.%E9%B8%A1%E8%9B%8B%E6%8E%89%E8%90%BD-%E4%B8%A4%E6%9E%9A%E9%B8%A1%E8%9B%8B/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142906976" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/egg-drop-with-2-eggs-and-n-floors/solutions/2949710/letmefly-1884ji-dan-diao-luo-liang-mei-j-saz6/" target="_blank">LeetCode题解</a>|
681+
|1895.最大的幻方|中等|<a href="https://leetcode.cn/problems/largest-magic-square/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/01/18/LeetCode%201895.%E6%9C%80%E5%A4%A7%E7%9A%84%E5%B9%BB%E6%96%B9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/157104818" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/largest-magic-square/solutions/3883805/letmefly-1895zui-da-de-huan-fang-bao-li-9ln7k/" target="_blank">LeetCode题解</a>|
681682
|1901.寻找峰值II|中等|<a href="https://leetcode.cn/problems/find-a-peak-element-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/19/LeetCode%201901.%E5%AF%BB%E6%89%BE%E5%B3%B0%E5%80%BCII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135083347" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-a-peak-element-ii/solutions/2572012/letmefly-1901xun-zhao-feng-zhi-iier-fen-19tmj/" target="_blank">LeetCode题解</a>|
682683
|1911.最大子序列交替和|中等|<a href="https://leetcode.cn/problems/maximum-alternating-subsequence-sum/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/11/LeetCode%201911.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%88%97%E4%BA%A4%E6%9B%BF%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131652316" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-alternating-subsequence-sum/solutions/2339029/letmefly-1911zui-da-zi-xu-lie-jiao-ti-he-fyzq/" target="_blank">LeetCode题解</a>|
683684
|1920.基于排列构建数组|简单|<a href="https://leetcode.cn/problems/build-array-from-permutation/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/06/LeetCode%201920.%E5%9F%BA%E4%BA%8E%E6%8E%92%E5%88%97%E6%9E%84%E5%BB%BA%E6%95%B0%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147748208" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/build-array-from-permutation/solutions/3670248/letmefly-1920ji-yu-pai-lie-gou-jian-shu-p0pif/" target="_blank">LeetCode题解</a>|
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
---
2+
title: 1895.最大的幻方:暴力中来点前缀和优化
3+
date: 2026-01-18 23:19:46
4+
tags: [题解, LeetCode, 中等, 数组, 矩阵, 前缀和]
5+
categories: [题解, LeetCode]
6+
index_img: https://assets.leetcode.com/uploads/2021/05/29/magicsquare-grid.jpg
7+
---
8+
9+
# 【LetMeFly】1895.最大的幻方:暴力中来点前缀和优化
10+
11+
力扣题目链接:[https://leetcode.cn/problems/largest-magic-square/](https://leetcode.cn/problems/largest-magic-square/)
12+
13+
<p>一个 <code>k x k</code> 的<strong> 幻方</strong> 指的是一个 <code>k x k</code> 填满整数的方格阵,且每一行、每一列以及两条对角线的和 <strong>全部</strong><strong>相等</strong> 。幻方中的整数 <strong>不需要互不相同</strong> 。显然,每个 <code>1 x 1</code> 的方格都是一个幻方。</p>
14+
15+
<p>给你一个 <code>m x n</code> 的整数矩阵 <code>grid</code> ,请你返回矩阵中 <strong>最大幻方</strong> 的 <strong>尺寸</strong> (即边长 <code>k</code>)。</p>
16+
17+
<p> </p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/29/magicsquare-grid.jpg" style="width: 413px; height: 335px;">
21+
<pre><b>输入:</b>grid = [[7,1,4,5,6],[2,5,1,6,4],[1,5,4,3,2],[1,2,7,3,4]]
22+
<b>输出:</b>3
23+
<b>解释:</b>最大幻方尺寸为 3 。
24+
每一行,每一列以及两条对角线的和都等于 12 。
25+
- 每一行的和:5+1+6 = 5+4+3 = 2+7+3 = 12
26+
- 每一列的和:5+5+2 = 1+4+7 = 6+3+3 = 12
27+
- 对角线的和:5+4+3 = 6+4+2 = 12
28+
</pre>
29+
30+
<p><strong>示例 2:</strong></p>
31+
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/29/magicsquare2-grid.jpg" style="width: 333px; height: 255px;">
32+
<pre><b>输入:</b>grid = [[5,1,3,1],[9,3,3,1],[1,3,3,8]]
33+
<b>输出:</b>2
34+
</pre>
35+
36+
<p> </p>
37+
38+
<p><strong>提示:</strong></p>
39+
40+
<ul>
41+
<li><code>m == grid.length</code></li>
42+
<li><code>n == grid[i].length</code></li>
43+
<li><code>1 &lt;= m, n &lt;= 50</code></li>
44+
<li><code>1 &lt;= grid[i][j] &lt;= 10<sup>6</sup></code></li>
45+
</ul>
46+
47+
48+
49+
## 解题方法:暴力中来点前缀和优化
50+
51+
最为暴力的做法是什么?
52+
53+
1. 枚举边长$k$(可从大到小枚举),复杂度$O(\min(m, n))$
54+
2. 枚举幻方左上角,复杂度$O(mn)$
55+
3. 对于一个方块,判断其是否为幻方,复杂度$O(k^2)$
56+
57+
总计复杂度$O(n^5)$,而$50^5=312,500,000=3.12e8$会超时,所以需要想办法优化一层循环。
58+
59+
不难发现,我们在判断一个方块是否为幻方时要暴力计算每一行之和,而一行的元素和使用前缀和即可$O(1)$算出,所以我们可以预处理计算出一个横向上的前缀和以及一个纵向上的前缀和(斜向由于只有两条所以无需前缀和加速)。
60+
61+
这样,时间复杂度就降低为了$O(n^4)$,而$50^4=6,250,000=6.25e6$可以接受。
62+
63+
+ 时间复杂度$O(mn\cdot\min(m,n))$
64+
+ 空间复杂度$O(mn)$
65+
66+
这道题要细心考虑边界问题。
67+
68+
### AC代码
69+
70+
#### C++
71+
72+
```cpp
73+
/*
74+
* @LastEditTime: 2026-01-18 23:17:50
75+
*/
76+
class Solution {
77+
private:
78+
vector<vector<int>> rowSum, colSum;
79+
80+
bool ok(vector<vector<int>>& grid, int x, int y, int l) {
81+
int cnt = 0, cntRev = 0;
82+
for (int i = 0; i < l; i++) {
83+
cnt += grid[x + i][y + i];
84+
cntRev += grid[x + i][y + l - i - 1]; // 易错
85+
}
86+
if (cnt != cntRev) {
87+
return false;
88+
}
89+
for (int i = 0; i < l; i++) {
90+
if (rowSum[x + i][y + l] - rowSum[x + i][y] != cnt) {
91+
return false;
92+
}
93+
if (colSum[x + l][y + i] - colSum[x][y + i] != cnt) {
94+
return false;
95+
}
96+
}
97+
return true;
98+
}
99+
public:
100+
int largestMagicSquare(vector<vector<int>>& grid) {
101+
int n = grid.size(), m = grid[0].size();
102+
rowSum.resize(n, vector<int>(m + 1));
103+
colSum.resize(n + 1, vector<int>(m));
104+
105+
for (int i = 0; i < n; i++) {
106+
for (int j = 0; j < m; j++) {
107+
rowSum[i][j + 1] = rowSum[i][j] + grid[i][j];
108+
colSum[i + 1][j] = colSum[i][j] + grid[i][j];
109+
}
110+
}
111+
112+
for (int k = n; k > 1; k--) {
113+
for (int i = 0; i + k <= n; i++) {
114+
for (int j = 0; j + k <= m; j++) {
115+
if (ok(grid, i, j, k)) {
116+
return k;
117+
}
118+
}
119+
}
120+
}
121+
return 1;
122+
}
123+
};
124+
```
125+
126+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/157104818)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/01/18/LeetCode%201895.%E6%9C%80%E5%A4%A7%E7%9A%84%E5%B9%BB%E6%96%B9/)~
127+
>
128+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-Accumulation-SomeTips.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ Interpreter: /bin/bash
383383

384384
</details>
385385

386+
### zsh里面 path和PATH是一个
387+
388+
ZSH中修改$path$变量也会自动修改$PATH$变量。
389+
386390
### Linux登录欢迎语motd
387391

388392
使用```ssh```登录Linux时会显示Linux欢迎语,据不完全测试,修改```/etc/motd```为你想要显示的内容即可。(比如看板娘)
@@ -637,6 +641,16 @@ Guid
637641
f35b2f66-3e03-4c9d-80b5-d72059a8735d
638642
```
639643

644+
### WOL(Wake on LAN)网络唤醒
645+
646+
归类到Windows下其实并不仅局限于Windows。
647+
648+
1. 固件支持WOL
649+
2. 局域网或公网可达
650+
3. 网卡有待机电
651+
4. 接收到魔术网络包(Magic Packe),内容`6 字节的 FF`+`目标网卡 MAC 地址 × 16 次`
652+
653+
没有认证,知道Mac地址就能发。
640654

641655
## About Phone
642656

todo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rm+rename 1895-largest-magic-square.cpp

0 commit comments

Comments
 (0)