当前位置:网站首页>Leetcode1380: lucky numbers in matrix
Leetcode1380: lucky numbers in matrix
2022-07-02 16:44:00 【Slow ploughing of stupid cattle】
Catalog
4. Are there multiple lucky numbers ?
1. Title Description
To give you one m * n Matrix , The number in the matrix Each are not identical . Please press arbitrarily Return all the lucky numbers in the matrix in order . Lucky number refers to the elements in the matrix that meet the following two conditions at the same time :
- The smallest of all elements in the same row
- The largest of all elements in the same column
Example 1:
Input :matrix = [[3,7,8],[9,11,13],[15,16,17]]
Output :[15]
explain :15 Is the only lucky number , Because it is the smallest value in its row , It is also the maximum value in the column .
Example 2:
Input :matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
Output :[12]
explain :12 Is the only lucky number , Because it is the smallest value in its row , It is also the maximum value in the column .
Example 3:
Input :matrix = [[7,8],[1,2]]
Output :[7]
Tips :
m == mat.length
n == mat[i].length
1 <= n, m <= 50
1 <= matrix[i][j] <= 10^5
All elements in the matrix are different
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
2. Problem solving analysis
Obviously , There is at most one lucky number in each line , At most, there is only one lucky number in each column .
The intuitive method is progressive scanning . Find the minimum value in the first row , Then confirm whether it is the maximum value of the column , If yes, it is a lucky number . Column by column scanning is also acceptable , The reason is the same .
Time complexity analysis
Make m and n Represents the number of rows and columns of input data respectively .
For each line , The time complexity to find the minimum value is
, The time complexity of finding the maximum value in the column where the minimum value is located is
, Therefore, the processing time complexity of each row is
. All in all m That's ok , So the total time complexity is
.
therefore , If the number of rows is greater than the number of columns , It should be advantageous to scan by column .
Spatial complexity analysis
During the search , In addition to storing a row minimum 、 The column where the row minimum value is located and the column maximum value , No additional storage required , So the space complexity is constant complexity .
3. Code implementation
class Solution:
def luckyNumbers(self, matrix: List[List[int]]) -> List[int]:
m = len(matrix) # number of row
n = len(matrix[0]) # number of col
rslt = []
for k in range(m): # Iteration over each row
# Find row min and also the min index
# rowmin,minidx = self.findMin(matrix[k])
rowmin = matrix[k][0]
rowminidx = 0
for col in range(1,n):
if matrix[k][col] < rowmin:
rowmin = matrix[k][col]
rowminidx = col
# Find the max of column[minidx]
colmax = matrix[0][rowminidx]
for k in range(1,m):
if matrix[k][rowminidx] > colmax:
colmax = matrix[k][rowminidx]
# Whether the row-min coincidence with col-max
if colmax == rowmin:
rslt.append(colmax)
return rslt if __name__ == '__main__':
sln = Solution()
matrix = [[3,7,8],[9,11,13],[15,16,17]]
print(sln.luckyNumbers(matrix))
matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
print(sln.luckyNumbers(matrix))
matrix = [[7,8],[1,2]]
print(sln.luckyNumbers(matrix))
matrix = [[7]] # corner case
print(sln.luckyNumbers(matrix))
matrix = [[8,7],[1,9]]
print(sln.luckyNumbers(matrix)) however , The above solution may be a little naive^-^. stay Leetcode The performance in submission is relatively poor :
# ======================================================================
# Execution time :96 ms, In all Python3 Defeated in submission 5.16% Users of
# Memory consumption :32.4 MB, In all Python3 Defeated in submission 5.16% Users of
# ======================================================================
Further efforts are needed .
4. Are there multiple lucky numbers ?
The Title Description implies that there may be multiple lucky numbers . But I want to design such a testcase When , It seems impossible to build such testcase Come on . I thought it over , It is believed that there should be no multiple lucky numbers that meet the requirements of this question .
Prove the following : Reduction to absurdity .
It's not general , hypothesis A[0][0] A lucky number , be A[0][0] Is the minimum value of the first line , meanwhile A[0][0] Is the maximum value of the first column . Suppose there is another number A[i][j](i>0, j>0), It's also a lucky number , By definition ,A[i][j] For the first time (i+1) The minimum value of the row , So I must be satisfied A[i][j]<A[0][j]<A[0][0]<A[0][j], This is related to A[i][j] It's a lucky number ( Column maximum ) The conclusion of is contradictory . Therefore, there cannot be more than one lucky number .
Based on this , The above program can be slightly optimized . That is, you can quit in advance after finding a lucky number .
边栏推荐
- 电脑管理员权限在哪里可以打开
- Text intelligent expansion and contraction control of swiftui text component (tutorial includes source code)
- 总结|机器视觉中三大坐标系及其相互关系
- Does bone conduction earphone have external sound? Advantages of bone conduction earphones
- 串口控制舵机转动
- OSPF - detailed explanation of NSSA area and full NSSA area (including configuration command), LSA type 7 lsa-7
- 图书管理系统(山东农业大学课程设计)
- [error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)
- LeetCode 1. 两数之和
- Take you ten days to easily complete the go micro service series (I)
猜你喜欢

PyC file decompile

请问怎么在oracle视图中使用stustr函数

pwm呼吸灯

Privacy computing technology innovation and industry practice seminar: Learning
![[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)](/img/c7/1949894e106036d2b412bcd6f70245.jpg)
[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)

Data security industry series Salon (III) | data security industry standard system construction theme Salon

Everyone Xinfu builds: a one-stop intelligent business credit service platform

流批一体在京东的探索与实践

Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)

La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
随机推荐
Résumé de l'entrevue de Dachang Daquan
The login box of unity hub becomes too narrow to log in
头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》
流批一体在京东的探索与实践
LeetCode 1. Sum of two numbers
Does bone conduction earphone have external sound? Advantages of bone conduction earphones
[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array
串口控制舵机转动
Exploration and practice of integration of streaming and wholesale in jd.com
[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)
Mysql database mysqldump why there is no statement to create a database
Yyds dry inventory executor package (parameter processing function)
Seal Library - installation and introduction
总结|机器视觉中三大坐标系及其相互关系
LeetCode 6. Zigzag transformation (n-shaped transformation)
渗透工具-内网权限维持-Cobalt strike
How to solve the failure of printer driver installation of computer equipment
unity Hub 登录框变得很窄 无法登录
学生选课系统(山东农业大学课程设计)
只是巧合?苹果iOS16的神秘技术竟然与中国企业5年前产品一致!