3070. Count Submatrices with Top-Left Element and Sum Less Than k LeetCode Solution
In this guide, you will get 3070. Count Submatrices with Top-Left Element and Sum Less Than k LeetCode Solution with the best time and space complexity. The solution to Count Submatrices with Top-Left Element and Sum Less Than k problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Count Submatrices with Top-Left Element and Sum Less Than k solution in C++
- Count Submatrices with Top-Left Element and Sum Less Than k solution in Java
- Count Submatrices with Top-Left Element and Sum Less Than k solution in Python
- Additional Resources

Problem Statement of Count Submatrices with Top-Left Element and Sum Less Than k
You are given a 0-indexed integer matrix grid and an integer k.
Return the number of submatrices that contain the top-left element of the grid, and have a sum less than or equal to k.
Example 1:
Input: grid = [[7,6,3],[6,6,1]], k = 18
Output: 4
Explanation: There are only 4 submatrices, shown in the image above, that contain the top-left element of grid, and have a sum less than or equal to 18.
Example 2:
Input: grid = [[7,2,9],[1,5,0],[2,6,6]], k = 20
Output: 6
Explanation: There are only 6 submatrices, shown in the image above, that contain the top-left element of grid, and have a sum less than or equal to 20.
Constraints:
m == grid.length
n == grid[i].length
1 <= n, m <= 1000
0 <= grid[i][j] <= 1000
1 <= k <= 109
Complexity Analysis
- Time Complexity: O(mn)
- Space Complexity: O(mn)
3070. Count Submatrices with Top-Left Element and Sum Less Than k LeetCode Solution in C++
class Solution {
public:
int countSubmatrices(vector<vector<int>>& grid, int k) {
const int m = grid.size();
const int n = grid[0].size();
int ans = 0;
vector<vector<int>> prefix(m + 1, vector<int>(n + 1));
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) {
prefix[i + 1][j + 1] =
grid[i][j] + prefix[i][j + 1] + prefix[i + 1][j] - prefix[i][j];
if (prefix[i + 1][j + 1] <= k)
++ans;
}
return ans;
}
};
/* code provided by PROGIEZ */
3070. Count Submatrices with Top-Left Element and Sum Less Than k LeetCode Solution in Java
class Solution {
public int countSubmatrices(int[][] grid, int k) {
final int m = grid.length;
final int n = grid[0].length;
int ans = 0;
int[][] prefix = new int[m + 1][n + 1];
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) {
prefix[i + 1][j + 1] = grid[i][j] + prefix[i][j + 1] + prefix[i + 1][j] - prefix[i][j];
if (prefix[i + 1][j + 1] <= k)
++ans;
}
return ans;
}
}
// code provided by PROGIEZ
3070. Count Submatrices with Top-Left Element and Sum Less Than k LeetCode Solution in Python
class Solution:
def countSubmatrices(self, grid: list[list[int]], k: int) -> int:
m = len(grid)
n = len(grid[0])
ans = 0
# prefix[i][j] := the sum of matrix[0..i)[0..j)
prefix = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(m):
for j in range(n):
prefix[i + 1][j + 1] = (grid[i][j] + prefix[i][j + 1] +
prefix[i + 1][j] - prefix[i][j])
if prefix[i + 1][j + 1] <= k:
ans += 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.