1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class Solution { public int[][] largestLocal(int[][] grid) { int n = grid.length;
int[][] res = new int[n - 2][n - 2]; for (int i = 0; i < res.length; i++) { for (int j = 0; j < res[i].length; j++) { int x = i + 1, y = j + 1; res[i][j] = max(grid, x, y); } }
return res; }
private int max(int[][] grid, int x, int y) { int max = grid[x][y]; max = Math.max(max, grid[x - 1][y]); max = Math.max(max, grid[x + 1][y]); max = Math.max(max, grid[x][y + 1]); max = Math.max(max, grid[x][y - 1]); max = Math.max(max, grid[x + 1][y + 1]); max = Math.max(max, grid[x + 1][y - 1]); max = Math.max(max, grid[x - 1][y + 1]); max = Math.max(max, grid[x - 1][y - 1]); return max; } }
|
References
2373. Largest Local Values in a Matrix