Similar Problems

Similar Problems not available

Largest Local Values In A Matrix - Leetcode Solution

Companies:

LeetCode:  Largest Local Values In A Matrix Leetcode Solution

Difficulty: Easy

Topics: matrix array  

Problem:

Given a matrix of integers, find all the local maximums and minimums in it. A local maximum is defined as an element whose value is greater than its neighbors. Similarly, a local minimum is defined as an element whose value is less than its neighbors. You need to return the positions of all such elements as an array of pairs (i, j) where i and j are 0-indexed row and column numbers of the matrix.

Example:

Input: matrix = [[5, 8, 6, 6], [7, 3, 5, 9], [3, 2, 6, 10], [4, 7, 4, 4]]

Output: [[0, 1], [1, 3], [2, 3], [3, 1], [3, 3]]

Explanation: The local maximums are: 8, 9, and 10 at positions [0, 1], [1, 3], and [2, 3] respectively. The local minimums are: 3 at position [1, 1] and 4 at positions [3, 1] and [3, 3].

Solution:

We can solve the problem by looping over all elements of the matrix and checking if they are greater than or less than their immediate neighbors. If an element satisfies the condition, we add its index to the result.

Code:

The following is the Python code for the solution.

class Solution:
    def getLocalValues(self, mat: List[List[int]]) -> List[List[int]]:
        if not mat:
            return []
        
        res = []
        rows, cols = len(mat), len(mat[0])
        
        for i in range(rows):
            for j in range(cols):
                val = mat[i][j]
                if i == 0:
                    if j == 0:
                        if val > mat[i + 1][j] and val > mat[i][j + 1]:
                            res.append([i, j])
                    elif j == cols - 1:
                        if val > mat[i + 1][j] and val > mat[i][j - 1]:
                            res.append([i, j])
                    else:
                        if val > mat[i + 1][j] and val > mat[i][j - 1] and val > mat[i][j + 1]:
                            res.append([i, j])
                elif i == rows - 1:
                    if j == 0:
                        if val > mat[i - 1][j] and val > mat[i][j + 1]:
                            res.append([i, j])
                    elif j == cols - 1:
                        if val > mat[i - 1][j] and val > mat[i][j - 1]:
                            res.append([i, j])
                    else:
                        if val > mat[i - 1][j] and val > mat[i][j - 1] and val > mat[i][j + 1]:
                            res.append([i, j])
                else:
                    if j == 0:
                        if val > mat[i - 1][j] and val > mat[i + 1][j] and val > mat[i][j + 1]:
                            res.append([i, j])
                    elif j == cols - 1:
                        if val > mat[i - 1][j] and val > mat[i + 1][j] and val > mat[i][j - 1]:
                            res.append([i, j])
                    else:
                        if val > mat[i - 1][j] and val > mat[i + 1][j] and val > mat[i][j - 1] and val > mat[i][j + 1]:
                            res.append([i, j])
                        
        return res

The getLocalValues function takes in a matrix as an input and returns a list of indices of all local maximums and minimums. We iterate over all elements of the matrix and check if they satisfy the local maximum and minimum conditions. If they do, we add their indices to the result list.

Largest Local Values In A Matrix Solution Code

1