Watch 10 video solutions for Find Sorted Submatrices With Maximum Element at Most K, a hard level problem involving Array, Stack, Matrix. This walkthrough by NeetCode has 331,616 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a 2D matrix grid of size m x n. You are also given a non-negative integer k.
Return the number of submatrices of grid that satisfy the following conditions:
k.A submatrix (x1, y1, x2, y2) is a matrix that forms by choosing all cells grid[x][y] where x1 <= x <= x2 and y1 <= y <= y2.
Example 1:
Input: grid = [[4,3,2,1],[8,7,6,1]], k = 3
Output: 8
Explanation:

The 8 submatrices are:
[[1]][[1]][[2,1]][[3,2,1]][[1],[1]][[2]][[3]][[3,2]]Example 2:
Input: grid = [[1,1,1],[1,1,1],[1,1,1]], k = 1
Output: 36
Explanation:
There are 36 submatrices of grid. All submatrices have their maximum element equal to 1.
Example 3:
Input: grid = [[1]], k = 1
Output: 1
Constraints:
1 <= m == grid.length <= 1031 <= n == grid[i].length <= 1031 <= grid[i][j] <= 1091 <= k <= 109
Problem Overview: Given a matrix and an integer k, count submatrices that satisfy two constraints: every row inside the submatrix remains non‑decreasing (sorted left to right) and the maximum element inside the submatrix is at most k.
Approach 1: Brute Force Submatrix Enumeration (O(m^2 * n^2), O(1) space)
Enumerate every possible submatrix using four boundaries. For each candidate, scan all cells to verify two conditions: values do not exceed k and every row remains non‑decreasing across the selected columns. This approach directly checks the definition but repeats large amounts of work because each submatrix is validated from scratch. It works for very small matrices but quickly becomes infeasible as matrix size grows.
Approach 2: Row Preprocessing + Monotonic Stack (O(m * n), O(n) space)
Preprocess each cell to compute the maximum width of a valid sorted segment ending at that column. While scanning a row, extend the width if matrix[i][j] >= matrix[i][j-1] and matrix[i][j] ≤ k; otherwise reset the width to 0. This converts the matrix into a grid where each cell represents the largest valid horizontal span of a sorted segment ending there.
Now treat each column as a histogram where the value is the width computed above. For each row while moving downward, use a monotonic stack to maintain increasing widths. The stack helps determine how many submatrices can end at the current row by aggregating contributions of previous rows while maintaining the minimum width constraint. This technique is similar to counting rectangles in histogram problems and avoids recomputing ranges repeatedly.
The key insight: for a submatrix to remain sorted, the limiting factor is the minimum valid width among its rows. The monotonic stack efficiently tracks these minima while extending submatrices vertically. Combined with matrix preprocessing from matrix traversal and standard array operations, the algorithm counts all valid rectangles in linear time.
Recommended for interviews: The monotonic stack solution. Interviewers expect you to recognize the transformation from matrix constraints to a histogram-style counting problem. Mentioning the brute force method first shows you understand the constraints, but implementing the O(m * n) stack approach demonstrates strong algorithmic problem solving.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Submatrix Enumeration | O(m^2 * n^2) | O(1) | Conceptual baseline or when matrix size is very small |
| Row Preprocessing + Monotonic Stack | O(m * n) | O(n) | General optimal solution for large matrices |