Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[7,6,3],[6,6,1]] 18
[[7,2,9],[1,5,0],[2,6,6]] 20
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3070: Count Submatrices with Top-Left Element and Sum Less Than k
class Solution {
public int countSubmatrices(int[][] grid, int k) {
int m = grid.length, n = grid[0].length;
int[][] s = new int[m + 1][n + 1];
int ans = 0;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1];
if (s[i][j] <= k) {
++ans;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #3070: Count Submatrices with Top-Left Element and Sum Less Than k
func countSubmatrices(grid [][]int, k int) (ans int) {
s := make([][]int, len(grid)+1)
for i := range s {
s[i] = make([]int, len(grid[0])+1)
}
for i, row := range grid {
for j, x := range row {
s[i+1][j+1] = s[i+1][j] + s[i][j+1] - s[i][j] + x
if s[i+1][j+1] <= k {
ans++
}
}
}
return
}
# Accepted solution for LeetCode #3070: Count Submatrices with Top-Left Element and Sum Less Than k
class Solution:
def countSubmatrices(self, grid: List[List[int]], k: int) -> int:
s = [[0] * (len(grid[0]) + 1) for _ in range(len(grid) + 1)]
ans = 0
for i, row in enumerate(grid, 1):
for j, x in enumerate(row, 1):
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + x
ans += s[i][j] <= k
return ans
// Accepted solution for LeetCode #3070: Count Submatrices with Top-Left Element and Sum Less Than k
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3070: Count Submatrices with Top-Left Element and Sum Less Than k
// class Solution {
// public int countSubmatrices(int[][] grid, int k) {
// int m = grid.length, n = grid[0].length;
// int[][] s = new int[m + 1][n + 1];
// int ans = 0;
// for (int i = 1; i <= m; ++i) {
// for (int j = 1; j <= n; ++j) {
// s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1];
// if (s[i][j] <= k) {
// ++ans;
// }
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3070: Count Submatrices with Top-Left Element and Sum Less Than k
function countSubmatrices(grid: number[][], k: number): number {
const m = grid.length;
const n = grid[0].length;
const s: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
let ans: number = 0;
for (let i = 1; i <= m; ++i) {
for (let j = 1; j <= n; ++j) {
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1];
if (s[i][j] <= k) {
++ans;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.