Leetcode 2373: Largest Local Values in a Matrix

grid47
grid47
Exploring patterns and algorithms
Mar 14, 2024 5 min read

You are given a square matrix grid of size n x n. Your task is to generate a new matrix maxLocal of size (n - 2) x (n - 2) where each element maxLocal[i][j] represents the largest value from a 3 x 3 submatrix in grid centered at (i + 1, j + 1). In other words, for each element of the new matrix, find the maximum value from its surrounding 3 x 3 region in the original grid.
Problem
Approach
Steps
Complexity
Input: The input consists of an n x n matrix grid.
Example: grid = [[3, 1, 2, 7], [6, 5, 8, 3], [9, 4, 0, 1], [7, 6, 5, 9]]
Constraints:
• 3 <= n <= 100
• 1 <= grid[i][j] <= 100
Output: Return the generated matrix maxLocal, where each element is the largest value from the corresponding 3x3 region in the original grid.
Example: Output: [[8, 8], [9, 8]]
Constraints:
Goal: The goal is to compute a new matrix where each element is the maximum of a 3x3 submatrix from the original matrix.
Steps:
• 1. Iterate through each possible 3x3 submatrix in the input matrix.
• 2. For each submatrix, find the maximum value within the 3x3 region.
• 3. Store the maximum value in the corresponding position in the new matrix.
Goal: The input matrix grid is guaranteed to have dimensions between 3 and 100, and all elements are between 1 and 100.
Steps:
• The matrix grid has a size between 3x3 and 100x100.
• Each element in the grid is an integer between 1 and 100.
Assumptions:
• The input grid will always have a size of at least 3x3.
Input: Input: grid = [[9, 9, 8, 1], [5, 6, 2, 6], [8, 2, 6, 4], [6, 2, 2, 2]]
Explanation: For this input, the largest 3x3 values are: [[9, 9], [8, 6]]. This corresponds to the largest values from every 3x3 region in the original grid.

Input: 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]]
Explanation: In this case, every 3x3 region contains a 2, so the output matrix is filled with 2.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus