Largest Local Values in a Matrix

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, find the largest value in every contiguous 3 x 3 matrix in grid. Return the generated matrix.

30:00

Largest Local Values in a Matrix
easy
Free written solution
Topics
Companies

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, find the largest value in every contiguous 3 x 3 matrix in grid. Return the generated matrix.

Example 1:
Input: {"grid":[[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]}
Output: [[9,9],[8,6]]
Constraints:
  • n==grid.length==grid[i].lengthn == \text{grid.length} == \text{grid}[i]\text{.length}

  • 3≤n≤1003 \leq n \leq 100

  • 1≤grid[i][j]≤1001 \leq \text{grid}[i][j] \leq 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"grid":[[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]}

Slide a 3 x 3 window over the 4 x 4 grid. Each window position writes its largest value into a 2 x 2 result.

grid 4 x 4

9
9
8
1
5
6
2
6
8
2
6
4
6
2
2
2

result 2 x 2

written 0 / 4

Variables
VariableValue
n4
size2
DepthFunction Call
Stack empty
0/13