Skip to content

Commit 907d670

Browse files
committed
update: 添加问题“3070.元素和小于等于k的子矩阵的数目”的代码和题解 (#1450)
3070: AC.cpp (#1449) - AC,91.79%,100.00% + (en) Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent c942818 commit 907d670

4 files changed

Lines changed: 135 additions & 1 deletion

.commitmsg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1727: AC.cpp(#1446) - AC,31.11%,44.44%
1+
3070: AC.cpp (#1449) - AC,91.79%,100.00% + (en)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-03-18 22:17:11
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-03-18 22:19:52
6+
*/
7+
#ifdef _DEBUG
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int countSubmatrices(vector<vector<int>>& grid, int k) {
14+
int ans = 0;
15+
int n = grid.size(), m = grid[0].size();
16+
for (int i = 0; i < n; i++) {
17+
for (int j = 0; j < m; j++) {
18+
if (i && j) {
19+
grid[i][j] += grid[i - 1][j] + grid[i][j - 1] - grid[i - 1][j - 1];
20+
} else if (i) {
21+
grid[i][j] += grid[i - 1][j];
22+
} else if (j) {
23+
grid[i][j] += grid[i][j - 1];
24+
}
25+
ans += grid[i][j] <= k;
26+
}
27+
}
28+
return ans;
29+
}
30+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,7 @@
10091009
|3066.超过阈值的最少操作数II|中等|<a href="https://leetcode.cn/problems/minimum-operations-to-exceed-threshold-value-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/01/15/LeetCode%203066.%E8%B6%85%E8%BF%87%E9%98%88%E5%80%BC%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%95%B0II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145160799" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-operations-to-exceed-threshold-value-ii/solutions/3048076/letmefly-mo-ni-yuan-di-jian-dui-o1kong-j-k4rj/" target="_blank">LeetCode题解</a>|
10101010
|3067.在带权树网络中统计可连接服务器对数目|中等|<a href="https://leetcode.cn/problems/count-pairs-of-connectable-servers-in-a-weighted-tree-network/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/04/LeetCode%203067.%E5%9C%A8%E5%B8%A6%E6%9D%83%E6%A0%91%E7%BD%91%E7%BB%9C%E4%B8%AD%E7%BB%9F%E8%AE%A1%E5%8F%AF%E8%BF%9E%E6%8E%A5%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%AF%B9%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139456087" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-pairs-of-connectable-servers-in-a-weighted-tree-network/solutions/2801125/letmefly-3067zai-dai-quan-shu-wang-luo-z-zf2j/" target="_blank">LeetCode题解</a>|
10111011
|3068.最大节点价值之和|困难|<a href="https://leetcode.cn/problems/find-the-maximum-sum-of-node-values/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/27/LeetCode%203068.%E6%9C%80%E5%A4%A7%E8%8A%82%E7%82%B9%E4%BB%B7%E5%80%BC%E4%B9%8B%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148267428" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-maximum-sum-of-node-values/solutions/3687719/letmefly-3068zui-da-jie-dian-jie-zhi-zhi-ixt6/" target="_blank">LeetCode题解</a>|
1012+
|3070.元素和小于等于k的子矩阵的数目|中等|<a href="https://leetcode.cn/problems/count-submatrices-with-top-left-element-and-sum-less-than-k/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/03/18/LeetCode%203070.%E5%85%83%E7%B4%A0%E5%92%8C%E5%B0%8F%E4%BA%8E%E7%AD%89%E4%BA%8Ek%E7%9A%84%E5%AD%90%E7%9F%A9%E9%98%B5%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/159214883" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-submatrices-with-top-left-element-and-sum-less-than-k/solutions/3930092/letmefly-3070yuan-su-he-xiao-yu-deng-yu-oezu8/" target="_blank">LeetCode题解</a>|
10121013
|3074.重新分装苹果|简单|<a href="https://leetcode.cn/problems/apple-redistribution-into-boxes/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/24/LeetCode%203074.%E9%87%8D%E6%96%B0%E5%88%86%E8%A3%85%E8%8B%B9%E6%9E%9C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/156239770" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/apple-redistribution-into-boxes/solutions/3864892/letmefly-3074zhong-xin-fen-zhuang-ping-g-99qr/" target="_blank">LeetCode题解</a>|
10131014
|3075.幸福值最大化的选择方案|中等|<a href="https://leetcode.cn/problems/maximize-happiness-of-selected-children/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/25/LeetCode%203075.%E5%B9%B8%E7%A6%8F%E5%80%BC%E6%9C%80%E5%A4%A7%E5%8C%96%E7%9A%84%E9%80%89%E6%8B%A9%E6%96%B9%E6%A1%88/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/156268485" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximize-happiness-of-selected-children/solutions/3865460/letmefly-3075xing-fu-zhi-zui-da-hua-de-x-skra/" target="_blank">LeetCode题解</a>|
10141015
|3083.字符串及其反转中是否存在同一子字符串|简单|<a href="https://leetcode.cn/problems/existence-of-a-substring-in-a-string-and-its-reverse/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/26/LeetCode%203083.%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%8F%8A%E5%85%B6%E5%8F%8D%E8%BD%AC%E4%B8%AD%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8%E5%90%8C%E4%B8%80%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144746598" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/existence-of-a-substring-in-a-string-and-its-reverse/solutions/3031052/letmefly-3083zi-fu-chuan-ji-qi-fan-zhuan-6mhd/" target="_blank">LeetCode题解</a>|
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
title: 3070.元素和小于等于 k 的子矩阵的数目:原地修改(前缀和思想)
3+
date: 2026-03-18 22:20:44
4+
tags: [题解, LeetCode, 中等, 数组, 矩阵, 前缀和]
5+
categories: [题解, LeetCode]
6+
index_img: https://assets.leetcode.com/uploads/2024/01/01/example1.png
7+
---
8+
9+
# 【LetMeFly】3070.元素和小于等于 k 的子矩阵的数目:原地修改(前缀和思想)
10+
11+
力扣题目链接:[https://leetcode.cn/problems/count-submatrices-with-top-left-element-and-sum-less-than-k/](https://leetcode.cn/problems/count-submatrices-with-top-left-element-and-sum-less-than-k/)
12+
13+
<p>给你一个下标从 <strong>0</strong> 开始的整数矩阵 <code>grid</code> 和一个整数 <code>k</code>。</p>
14+
15+
<p>返回包含 <code>grid</code> 左上角元素、元素和小于或等于 <code>k</code> 的 <strong><span data-keyword="submatrix">子矩阵</span></strong>的数目。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong class="example">示例 1:</strong></p>
20+
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/01/example1.png" style="padding: 10px; background: #fff; border-radius: .5rem;" />
21+
<pre>
22+
<strong>输入:</strong>grid = [[7,6,3],[6,6,1]], k = 18
23+
<strong>输出:</strong>4
24+
<strong>解释:</strong>如上图所示,只有 4 个子矩阵满足:包含 grid 的左上角元素,并且元素和小于或等于 18 。</pre>
25+
26+
<p><strong class="example">示例 2:</strong></p>
27+
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/01/example21.png" style="padding: 10px; background: #fff; border-radius: .5rem;" />
28+
<pre>
29+
<strong>输入:</strong>grid = [[7,2,9],[1,5,0],[2,6,6]], k = 20
30+
<strong>输出:</strong>6
31+
<strong>解释:</strong>如上图所示,只有 6 个子矩阵满足:包含 grid 的左上角元素,并且元素和小于或等于 20 。
32+
</pre>
33+
34+
<p>&nbsp;</p>
35+
36+
<p><strong>提示:</strong></p>
37+
38+
<ul>
39+
<li><code>m == grid.length </code></li>
40+
<li><code>n == grid[i].length</code></li>
41+
<li><code>1 &lt;= n, m &lt;= 1000 </code></li>
42+
<li><code>0 &lt;= grid[i][j] &lt;= 1000</code></li>
43+
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
44+
</ul>
45+
46+
47+
48+
## 解题方法:前缀和原地修改
49+
50+
第一层循环从上到下第二层循环从左到右遍历一遍$grid$数组,在遍历过程中把$grid[i][j]$的值修改为从左上角到这个元素的子矩阵元素之和。
51+
52+
怎么$O(1)$时间得到右下角为$(i,j)$的子矩阵之和?借助前面的遍历结果即可:
53+
54+
+ 如果$i$和$j$都大于$0$,则$grid[i][j] += grid[i - 1][j] + grid[i][j - 1] - grid[i - 1][j - 1]$:
55+
56+
```
57+
1 2
58+
3 4
59+
```
60+
61+
如图矩阵在计算左上角到右下角的$4$的时候,可以借助左上角到$4$上面$2$的元素和 + 左上角到$4$左边$3$的元素和 - 计算重复的左上角到$4$左上角$1$的元素和。
62+
63+
+ 如果$i$大于$0$而$j$等于$0$,则直接加上这个元素上一行的结果即可;
64+
+ 如果$j$大于$0$而$i$等于$0$同理。
65+
66+
优化:如果到$grid[i][j]$的子矩阵元素和已经大于$k$,那么再往右和往下的更大子矩阵的和一定更大,可跳过。
67+
68+
+ 时间复杂度$O(mn)$
69+
+ 空间复杂度$O(1)$
70+
71+
### AC代码
72+
73+
#### C++
74+
75+
```cpp
76+
/*
77+
* @LastEditTime: 2026-03-18 22:19:52
78+
*/
79+
class Solution {
80+
public:
81+
int countSubmatrices(vector<vector<int>>& grid, int k) {
82+
int ans = 0;
83+
int n = grid.size(), m = grid[0].size();
84+
for (int i = 0; i < n; i++) {
85+
for (int j = 0; j < m; j++) {
86+
if (i && j) {
87+
grid[i][j] += grid[i - 1][j] + grid[i][j - 1] - grid[i - 1][j - 1];
88+
} else if (i) {
89+
grid[i][j] += grid[i - 1][j];
90+
} else if (j) {
91+
grid[i][j] += grid[i][j - 1];
92+
}
93+
ans += grid[i][j] <= k;
94+
}
95+
}
96+
return ans;
97+
}
98+
};
99+
```
100+
101+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/159214883)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/03/18/LeetCode%203070.%E5%85%83%E7%B4%A0%E5%92%8C%E5%B0%8F%E4%BA%8E%E7%AD%89%E4%BA%8Ek%E7%9A%84%E5%AD%90%E7%9F%A9%E9%98%B5%E7%9A%84%E6%95%B0%E7%9B%AE/)~
102+
>
103+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

0 commit comments

Comments
 (0)