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.
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.
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) ]