3070. Count Submatrices with Top-Left Element and Sum Less Than k
Difficulty: Medium
Topics: Senior, Array, Matrix, Prefix Sum, Weekly Contest 387
You are given a 0-indexed integer matrix grid and an integer k.
Return the number of submatrices[^1] 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] <= 10001 <= k <= 10⁹
Solution:
The problem asks to count all submatrices that include the top‑left cell (0,0) and whose sum is less than or equal to a given integer k. The provided solution uses an efficient prefix‑sum technique: it iterates over the matrix row by row, maintaining a running column‑wise prefix that represents the sum of the submatrix from (0,0) to the current cell (i,j). For each such cell, if its corresponding sum is ≤ k, the counter is incremented. This yields the total number of valid submatrices in O(m·n) time and O(n) extra space.
Approach:
- Initialize an array
colPrefixof sizen(number of columns) with zeros. - Set a counter
count = 0. - Loop over each row
ifrom0tom-1:- Initialize a variable
rowSum = 0. - Loop over each column
jfrom0ton-1:- Add
grid[i][j]torowSum→ this gives the sum of the current row from column0toj. - Add
rowSumtocolPrefix[j]. After this addition,colPrefix[j]holds the sum of the rectangle whose top‑left is(0,0)and bottom‑right is(i,j). - If
colPrefix[j] <= k, incrementcount.
- Add
- Initialize a variable
- Return
count.
Let's implement this solution in PHP: 3070. Count Submatrices with Top-Left Element and Sum Less Than k
<?php
/**
* @param Integer[][] $grid
* @param Integer $k
* @return Integer
*/
function countSubmatrices(array $grid, int $k): int
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo countSubmatrices([[7,6,3],[6,6,1]], 18) . "\n"; // Output: 4
echo countSubmatrices([[7,2,9],[1,5,0],[2,6,6]], 20) . "\n"; // Output: 6
?>
Explanation:
- Because every valid submatrix must start at
(0,0), it is completely determined by its bottom‑right corner(i,j). - Instead of recomputing each rectangle sum from scratch, we build cumulative sums incrementally:
-
rowSumaccumulates the sum of the current row up to columnj. -
colPrefix[j]accumulates the sum of all rows up to rowifor the rectangle that ends at columnj. Initially, fori=0, it becomes the row prefix of the first row. For later rows, adding the newrowSumeffectively appends the new row’s segment to the previous cumulative sum, forming the complete rectangle sum.
-
- The algorithm checks every possible bottom‑right corner exactly once, ensuring correctness and efficiency.
Complexity
- Time Complexity: O(m·n) – each cell is processed a constant number of times.
-
Space Complexity: O(n) – the
colPrefixarray of lengthn(plus a few scalar variables). This is optimal for the problem constraints (up to 1000 columns).
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:


Top comments (0)