Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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.
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]]
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.
Example 2:
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.
Constraints:
n == grid.length == grid[i].length
3 <= n <= 100
1 <= grid[i][j] <= 100
public int[][] largestLocal(int[][] grid) {
int[][] result = new int[grid.length - 2][grid.length - 2];
for (int i = 0; i < result.length; ++i) {
for (int j = 0; j < result.length; ++j) {
int largest = Integer.MIN_VALUE;
for (int row = i; row < i + 3; ++row) {
for (int column = j; column < j + 3; ++column) {
largest = Math.max(largest, grid[row][column]);
}
}
result[i][j] = largest;
}
}
return result;
}
vector<vector<int>> largestLocal(vector<vector<int>>& grid) {
vector<vector<int>> result(grid.size() - 2, vector<int>(grid.size() - 2));
for (int i = 0; i < result.size(); ++i) {
for (int j = 0; j < result.size(); ++j) {
int largest = INT_MIN;
for (int row = i; row < i + 3; ++row) {
for (int column = j; column < j + 3; ++column) {
largest = max(largest, grid[row][column]);
}
}
result[i][j] = largest;
}
}
return result;
}
class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
n = len(grid)
ans = []
for i in range(n - 2):
res = []
for j in range(n - 2):
k = []
k.append(grid[i][j])
k.append(grid[i][j + 1])
k.append(grid[i][j + 2])
k.append(grid[i + 1][j])
k.append(grid[i + 1][j + 1])
k.append(grid[i + 1][j + 2])
k.append(grid[i + 2][j])
k.append(grid[i + 2][j + 1])
k.append(grid[i + 2][j + 2])
m = max(k)
res.append(m)
ans.append(res)
return ans
In our experience, we suggest you solve this Largest Local Values in a Matrix LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Largest Local Values in a Matrix LeetCode Solution
I hope this Largest Local Values in a Matrix LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>