LeetCode #1380. Lucky Numbers in a Matrix

题目

1380. Lucky Numbers in a Matrix


解题方法

遍历矩阵,将每一行的最小值存入字典,键为行下标,值为列下标。然后遍历字典的键,再遍历它所在的列,如果不是最小值就break当前循环,否则循环结束后把这个lucky number加到返回值rat中。
时间复杂度:O(mn)
空间复杂度:O(m)


代码

class Solution:
    def luckyNumbers (self, matrix: List[List[int]]) -> List[int]:
        dic = {}
        for i in range(len(matrix)):
            tempmin = matrix[i][0]
            dic[i] = 0
            for j in range(1, len(matrix[i])):
                if matrix[i][j] < tempmin:
                    tempmin = matrix[i][j]
                    dic[i] = j
        
        rat = []
        for key in dic.keys():
            for i in range(len(matrix)):
                if matrix[i][dic[key]] > matrix[key][dic[key]]:
                    break
            else:
                rat.append(matrix[key][dic[key]])
        return rat
上一篇:LeetCode #1266. Minimum Time Visiting All Points


下一篇:LeetCode #1427. Perform String Shifts