Taro Logo

Largest Local Values in a Matrix

Easy
Meta logo
Meta
1 view
Topics:
Arrays

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.

For example, given the following input:

grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]

The expected output would be:

[[9,9],[8,6]]

Another example:

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]]

Expected output:

[[2,2,2],[2,2,2],[2,2,2]]

Write a function to solve this problem and discuss its time and space complexity. Also, discuss any potential edge cases and how your solution handles them.

Solution


Brute Force Solution

Overview

The most straightforward approach is to iterate through each possible 3x3 submatrix within the given grid and find the maximum value within that submatrix. This maximum value is then placed in the corresponding position in the maxLocal matrix.

Algorithm

  1. Initialize the maxLocal matrix with dimensions (n-2) x (n-2). This matrix will store the maximum values of each 3x3 submatrix.
  2. Iterate through the grid matrix from i = 0 to n-2 and j = 0 to n-2.
  3. For each (i, j) position, find the maximum value in the 3x3 submatrix centered at (i+1, j+1).
  4. Assign this maximum value to maxLocal[i][j].
  5. Return the maxLocal matrix.

Code

def max_local_brute_force(grid):
    n = len(grid)
    max_local = [[0] * (n - 2) for _ in range(n - 2)]

    for i in range(n - 2):
        for j in range(n - 2):
            max_val = 0
            for x in range(i, i + 3):
                for y in range(j, j + 3):
                    max_val = max(max_val, grid[x][y])
            max_local[i][j] = max_val

    return max_local

Time and Space Complexity

  • Time Complexity: O((n-2)^2 * 3 * 3) = O(n^2). For each element in the maxLocal matrix, we iterate through a 3x3 submatrix.
  • Space Complexity: O((n-2)^2) = O(n^2) to store the maxLocal matrix.

Optimal Solution

Overview

This solution is essentially the same as the brute force, as there is no apparent overlapping subproblem or optimal substructure to exploit with dynamic programming. The brute force is already quite efficient. There aren't any tricks to reduce the number of iterations required.

Algorithm

The algorithm remains the same as the brute force approach:

  1. Initialize the maxLocal matrix with dimensions (n-2) x (n-2). This matrix will store the maximum values of each 3x3 submatrix.
  2. Iterate through the grid matrix from i = 0 to n-2 and j = 0 to n-2.
  3. For each (i, j) position, find the maximum value in the 3x3 submatrix centered at (i+1, j+1).
  4. Assign this maximum value to maxLocal[i][j].
  5. Return the maxLocal matrix.

Code

def max_local_optimal(grid):
    n = len(grid)
    max_local = [[0] * (n - 2) for _ in range(n - 2)]

    for i in range(n - 2):
        for j in range(n - 2):
            max_val = 0
            for x in range(i, i + 3):
                for y in range(j, j + 3):
                    max_val = max(max_val, grid[x][y])
            max_local[i][j] = max_val

    return max_local

Time and Space Complexity

  • Time Complexity: O((n-2)^2 * 3 * 3) = O(n^2). For each element in the maxLocal matrix, we iterate through a 3x3 submatrix.
  • Space Complexity: O((n-2)^2) = O(n^2) to store the maxLocal matrix.

Edge Cases

  • The problem states that 3 <= n <= 100, so we don't need to handle cases where n is less than 3. If n were less than 3, we could throw an exception or return an empty matrix, depending on the requirements.
  • If the input grid is None or empty, we might want to return an empty matrix or throw an exception.