Largest Local Values in a Matrix

Easy

Description

Given an n x n integer matrix grid, generate an (n-2) x (n-2) matrix maxLocal where maxLocal[i][j] is the largest value of the 3 x 3 matrix centered at grid[i+1][j+1].

Examples

Input:grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
Output:[[9,9],[8,6]]
Explanation:

Max in each 3x3.

Input:grid = [[7,4,6],[2,8,1],[9,3,5]]
Output:[[8]]
Explanation:

With a 3x3 input grid, there is only one possible 3x3 submatrix (the entire grid itself). The maximum value among all elements [7,4,6,2,8,1,9,3,5] is 9, so the output is [[9]].

Input:grid = [[10,5,3,8],[7,12,9,4],[6,1,15,2],[11,8,6,13]]
Output:[[12,15],[15,15]]
Explanation:

For the top-left 3x3 submatrix [[10,5,3],[7,12,9],[6,1,15]], the maximum is 15. For the top-right 3x3 submatrix [[5,3,8],[12,9,4],[1,15,2]], the maximum is 15. For the bottom-left 3x3 submatrix [[7,12,9],[6,1,15],[11,8,6]], the maximum is 15. For the bottom-right 3x3 submatrix [[12,9,4],[1,15,2],[8,6,13]], the maximum is 15.

Constraints

  • 3 ≤ n ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!