Watch 10 video solutions for Count Submatrices with Top-Left Element and Sum Less Than k, a medium level problem involving Array, Matrix, Prefix Sum. This walkthrough by codestorywithMIK has 7,317 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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].length1 <= n, m <= 1000 0 <= grid[i][j] <= 10001 <= k <= 109Problem Overview: You are given a 2D grid of integers and a value k. Count how many submatrices start at the top-left corner (0,0) and have a total sum less than or equal to k. Each valid submatrix is defined by choosing any bottom-right cell and computing the sum of elements inside the rectangle from (0,0) to that cell.
Approach 1: Brute Force with Cumulative Prefix Sum (Time: O(m*n), Space: O(m*n))
The direct way to evaluate each candidate submatrix is to compute the sum of the rectangle ending at every cell. A prefix sum matrix makes this efficient. Build a 2D prefix array where prefix[i][j] stores the sum of the rectangle from (0,0) to (i,j). Once the table is built, every bottom-right cell instantly represents the sum of a submatrix whose top-left corner is fixed. Iterate through the grid, check whether prefix[i][j] <= k, and increment the count. This approach is simple and reliable because it converts repeated submatrix summations into constant-time lookups. It’s a common technique when working with cumulative sums in a matrix.
Approach 2: Optimized Summation with 1D Prefix Array (Time: O(m*n), Space: O(n))
You can reduce memory by avoiding the full 2D prefix table. Instead, maintain a running prefix while iterating row by row. Keep a 1D array where each index stores the cumulative column sum up to the current row. As you move across a row, maintain another running sum that represents the total rectangle sum from (0,0) to the current cell. Each step effectively reconstructs the same value that a 2D prefix table would provide, but with less space. If the running sum is less than or equal to k, increment the result. This approach works well because the rectangle always starts at the same origin, allowing incremental accumulation across rows and columns. It relies heavily on efficient traversal of a 2D array.
Recommended for interviews: Interviewers typically expect the prefix-sum insight. Showing the brute-force idea first demonstrates you understand the structure of submatrix sums. Transitioning to a prefix-sum solution proves you know how to eliminate repeated calculations and reduce the time per query to constant time. The optimized 1D variant is a good follow-up if memory usage becomes a concern.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force with 2D Prefix Sum | O(m*n) | O(m*n) | Best for clarity and quick implementation when memory is not constrained |
| Optimized 1D Prefix Accumulation | O(m*n) | O(n) | Useful when reducing auxiliary memory while keeping linear traversal |