# LeetCode 1380. 矩阵中的幸运数 Lucky Numbers in a Matrix

### 一、中文版

m == mat.length

n == mat[i].length

1 <= n, m <= 50

1 <= matrix[i][j] <= 10^5

### 二、英文版

Given a m * n matrix of distinct numbers, return all lucky numbers in the matrix in any order.
A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.
Example 1:
Input: matrix = [[3,7,8],[9,11,13],[15,16,17]]
Output: [15]
Explanation: 15 is the only lucky number since it is the minimum in its row and the maximum in its column
Example 2:
Input: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
Output: [12]
Explanation: 12 is the only lucky number since it is the minimum in its row and the maximum in its column.
Example 3:
Input: matrix = [[7,8],[1,2]]
Output: [7]
Constraints:
m == mat.length
n == mat[i].length
1 <= n, m <= 50
1 <= matrix[i][j] <= 10^5.
All elements in the matrix are distinct.

class Solution:
def luckyNumbers (self, matrix: List[List[int]]) -> List[int]:
res = []
minnum_list = {}
#         找到每一行的最小值，存入字典，key是数字，value是该数字的下标
for i in range(len(matrix)):
# min_line = min(matrix[i])
min_num = matrix[i][0]
column = 0
for j in range(len(matrix[i])):
if matrix[i][j] < min_num:
min_num = matrix[i][j]
column = j
minnum_list[min_num] = [i,column]
print("minnum_list:",minnum_list)
# 遍历字典
flag = 0
for key in minnum_list:
max_col = minnum_list[key][1] # 找到对应列
print("max_col:",max_col)
#   遍历该列，判断 key 是否为最大值
for row in range(len(matrix)):
if matrix[row][max_col] > key:
flag = 1
break
if flag == 0:
res.append(key)
else:
flag = 0
continue
return res
"""
[[3,7,8],[9,11,13],[15,16,17]]
[[1,10,4,2],[9,3,8,7],[15,16,17,12]]
[[7,8],[1,2]]
"""

### 四、解题报告

|
7天前
|

【经典LeetCode算法题目专栏分类】【第6期】二分查找系列：x的平方根、有效完全平方数、搜索二位矩阵、寻找旋转排序数组最小值
【经典LeetCode算法题目专栏分类】【第6期】二分查找系列：x的平方根、有效完全平方数、搜索二位矩阵、寻找旋转排序数组最小值
9 0
|
7天前
|

【经典LeetCode算法题目专栏分类】【第4期】BFS广度优先算法：单词接龙、最小基因变化、二进制矩阵中的最短路径
【经典LeetCode算法题目专栏分类】【第4期】BFS广度优先算法：单词接龙、最小基因变化、二进制矩阵中的最短路径
6 0
|
10天前
|

LeetCode题目74:搜索二维矩阵
LeetCode题目74:搜索二维矩阵
4 0
|
11天前
|

LeetCode题目73:矩阵置零
LeetCode题目73:矩阵置零
23 12
|
15天前
|

22 0
|
6天前
|

【数据结构与算法】：关于时间复杂度与空间复杂度的计算（C/C++篇）——含Leetcode刷题-2
【数据结构与算法】：关于时间复杂度与空间复杂度的计算（C/C++篇）——含Leetcode刷题
14 2
|
6天前
|

【数据结构与算法】：关于时间复杂度与空间复杂度的计算（C/C++篇）——含Leetcode刷题-1
【数据结构与算法】：关于时间复杂度与空间复杂度的计算（C/C++篇）——含Leetcode刷题
18 2
|
7天前
【LeetCode刷题】前缀和解决问题：742.寻找数组的中心下标、238.除自身以外数组的乘积
【LeetCode刷题】前缀和解决问题：742.寻找数组的中心下标、238.除自身以外数组的乘积
16 0
|
7天前
【LeetCode刷题】二分查找：寻找旋转排序数组中的最小值、点名
【LeetCode刷题】二分查找：寻找旋转排序数组中的最小值、点名
16 0
|
7天前
|

【LeetCode刷题】二分查找：山脉数组的峰顶索引、寻找峰值
【LeetCode刷题】二分查找：山脉数组的峰顶索引、寻找峰值
10 1