1380. Lucky Numbers in a Matrix

Information

  • Diffculty: Easy

  • Created: 2020-03-15 11:54:00

  • Last Motified: 2020-03-15 11:54:17

  • Tags: Array

Intuition

First, we need to found the minimum value of each row and maximum value of each column. Then we could iterate each node to see if it is both the minimum of its column and the maximum of its row.

Solution

class Solution:
    def luckyNumbers (self, matrix: List[List[int]]) -> List[int]:
        m = len(matrix)
        n = len(matrix[0])
        min_row = [float('inf')] * m
        max_col = [float('-inf')] * n

        for i in range(m):
            for j in range(n):
                min_row[i] = min(min_row[i], matrix[i][j])
                max_col[j] = max(max_col[j], matrix[i][j])

        lucky_numbers = []
        for i in range(m):
            for j in range(n):
                if matrix[i][j] == min_row[i] == max_col[j]:
                    lucky_numbers.append(matrix[i][j])
        return lucky_numbers