> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Largest Local Values in a Matrix

> Tested Python solution for LeetCode 2373 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 2373, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/largest-local-values-in-a-matrix/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2373   # by problem number
lcpy gen -s largest_local_values_in_a_matrix   # by problem name
```

## Problem

You are given an n x n integer matrix grid.

Generate an integer matrix maxLocal of size (n - 2) x (n - 2) such that:

* maxLocal\[i]\[j] is equal to the largest value of the 3 x 3 matrix in grid centered around row i + 1 and column j + 1.

In other words, we want to find the largest value in every contiguous 3 x 3 matrix in grid.

Return the generated matrix.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2022/06/21/ex1.png)

```
Input: grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
Output: [[9,9],[8,6]]
Explanation: The diagram above shows the original matrix and the generated matrix.
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.
```

![Example 2](https://assets.leetcode.com/uploads/2022/07/02/ex2new2.png)

```
Input: grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
Output: [[2,2,2],[2,2,2],[2,2,2]]
Explanation: Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
```

### Constraints

* n == grid.length == grid\[i].length
* 3 \<= n \<= 100
* 1 \<= grid\[i]\[j] \<= 100

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/largest_local_values_in_a_matrix/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/largest_local_values_in_a_matrix/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2) - each of the (n - 2)^2 windows scans a fixed 3 x 3 area
    # Space: O(1) extra - excluding the (n - 2) x (n - 2) output matrix
    def largest_local(self, grid: list[list[int]]) -> list[list[int]]:
        n = len(grid)
        return [
            [max(grid[i + a][j + b] for a in range(3) for b in range(3)) for j in range(n - 2)]
            for i in range(n - 2)
        ]
```

## Complexity

| Time | Space |
| - | - |
| O(n^2) - each of the (n - 2)^2 windows scans a fixed 3 x 3 area | O(1) extra - excluding the (n - 2) x (n - 2) output matrix |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.