[LeetCode] Largest Local Values in a Matrix

2373. 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, we want to find the largest value in every contiguous 3 x 3 matrix in grid.

Return the generated matrix.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
vector<vector<int>> largestLocal(vector<vector<int>>& grid) {
int n = grid.size(), m = grid[0].size();
vector<vector<int>> res(n-2, vector<int>(m-2, INT_MIN));
for(int i = 0; i < n - 2; i++) {
for(int j = 0; j < m - 2; j++) {
for(int k = 0; k < 3; k++) {
for(int l = 0; l < 3; l++) {
res[i][j] = max(res[i][j], grid[i+k][j+l]);
}
}
}
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/14/PS/Codeforces/largest-local-values-in-a-matrix/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.