Largest Local Values in a Matrix (LeetCode 2373): Solution and Explanation
For every 3 x 3 block of an n x n grid, record the block's largest value. The answers form an (n - 2) x (n - 2) matrix.
Intuition
The window centered at (i + 1, j + 1) covers rows i..i+2 and columns j..j+2, so its top-left corner is (i, j). That makes output cell (i, j) line up directly with the window's corner.
A 3 x 3 window fits at n - 2 starting rows and n - 2 starting columns, which is exactly the size of the output.
Each window has only 9 cells, so scanning all of them is constant work. With n at most 100 there is no need for anything smarter.
Approach: Slide a 3 x 3 window
- Create
reswith n - 2 rows and n - 2 columns. - For each top-left corner (i, j), set
best = 0. - Scan rows i..i+2 and columns j..j+2, keeping the largest value in
best. - Write
res[i][j] = best. - Return
res.
Step-by-step example
grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
- n = 4, so the result is 2 x 2.
- Window at (0, 0) covers 9,9,8 / 5,6,2 / 8,2,6. Its max is 9, so res[0][0] = 9.
- Window at (0, 1) covers 9,8,1 / 6,2,6 / 2,6,4. Its max is 9, so res[0][1] = 9.
- Window at (1, 0) covers 5,6,2 / 8,2,6 / 6,2,2. Its max is 8, so res[1][0] = 8.
- Window at (1, 1) covers 6,2,6 / 2,6,4 / 2,2,2. Its max is 6, so res[1][1] = 6.
- Return [[9,9],[8,6]].
Code: Python, Java, C++
1class Solution:
2 def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
3 n = len(grid)
4 res = [[0] * (n - 2) for _ in range(n - 2)]
5 for i in range(n - 2):
6 for j in range(n - 2):
7 best = 0
8 for r in range(i, i + 3):
9 for c in range(j, j + 3):
10 best = max(best, grid[r][c])
11 res[i][j] = best
12 return res1class Solution {
2 public int[][] largestLocal(int[][] grid) {
3 int n = grid.length;
4 int[][] res = new int[n - 2][n - 2];
5 for (int i = 0; i < n - 2; i++) {
6 for (int j = 0; j < n - 2; j++) {
7 int best = 0;
8 for (int r = i; r < i + 3; r++) {
9 for (int c = j; c < j + 3; c++) {
10 best = Math.max(best, grid[r][c]);
11 }
12 }
13 res[i][j] = best;
14 }
15 }
16 return res;
17 }
18}1class Solution {
2public:
3 vector<vector<int>> largestLocal(vector<vector<int>>& grid) {
4 int n = grid.size();
5 vector<vector<int>> res(n - 2, vector<int>(n - 2, 0));
6 for (int i = 0; i < n - 2; i++) {
7 for (int j = 0; j < n - 2; j++) {
8 int best = 0;
9 for (int r = i; r < i + 3; r++) {
10 for (int c = j; c < j + 3; c++) {
11 best = max(best, grid[r][c]);
12 }
13 }
14 res[i][j] = best;
15 }
16 }
17 return res;
18 }
19};Time and space complexity
O(n^2)
There are (n - 2)^2 windows and each reads 9 cells, a constant.
O(1)
Only a running max is kept besides the (n - 2) x (n - 2) output, which the problem requires.
Other approaches
Row maxima first, then column maxima (time O(n^2), space O(n^2))
Compute an n x (n - 2) matrix of horizontal 3-wide maxima, then take vertical 3-tall maxima of that. Each output cell then costs 2 comparisons instead of 8. This is how a k x k max filter stays fast when k grows.
Monotonic deque per row and column (time O(n^2), space O(n^2))
For a general k x k window, run sliding-window maximum with a deque along each row, then along each column of the row results. The cost per cell no longer depends on k.
Common mistakes
- Indexing the window from its center and going out of bounds. Using the top-left corner (i, j) with i, j in 0..n-3 keeps every read inside the grid.
- Making the output n x n instead of (n - 2) x (n - 2).
- Starting
bestat a value that could exceed the real max. Values are at least 1, so 0 is safe, or start from grid[i][j].
Frequently asked questions
Why is the output size n - 2?
A 3-wide window needs 2 more cells past its start, so the last valid start is n - 3. Starts 0 through n - 3 give n - 2 positions per dimension.
Is the window centered or anchored at the corner?
The problem describes it as centered at (i + 1, j + 1). That is the same window as the one whose top-left corner is (i, j), which is easier to loop over.