Largest Local Values in a Matrix (LeetCode 2373): Solution and Explanation

Easy
Array
Matrix

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

  1. Create res with n - 2 rows and n - 2 columns.
  2. For each top-left corner (i, j), set best = 0.
  3. Scan rows i..i+2 and columns j..j+2, keeping the largest value in best.
  4. Write res[i][j] = best.
  5. Return res.

Step-by-step example

grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
  1. n = 4, so the result is 2 x 2.
  2. Window at (0, 0) covers 9,9,8 / 5,6,2 / 8,2,6. Its max is 9, so res[0][0] = 9.
  3. Window at (0, 1) covers 9,8,1 / 6,2,6 / 2,6,4. Its max is 9, so res[0][1] = 9.
  4. Window at (1, 0) covers 5,6,2 / 8,2,6 / 6,2,2. Its max is 8, so res[1][0] = 8.
  5. Window at (1, 1) covers 6,2,6 / 2,6,4 / 2,2,2. Its max is 6, so res[1][1] = 6.
  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 res
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n^2)

There are (n - 2)^2 windows and each reads 9 cells, a constant.

Space

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 best at 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.