当前位置:网站首页>1380. lucky numbers in matrices
1380. lucky numbers in matrices
2022-06-30 02:14:00 【_ Alkaid_】
difficulty : Simple
Catalog
2、 Time complexity and Spatial complexity
One 、 Problem description
I'm going to use LeetCode The description above .
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
Here is an example :

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
Two 、 Ideas
1、 Their thinking
What I'm using here is progressive scanning , Scan one line at a time , Seeking to travel The smallest number , Returns the minimum number of the row and its subscript . Subscript according to the minimum number of rows , Find the largest number in the column , If meet :
- The smallest of all elements in the same row
- The largest of all elements in the same column
that , Save the element in a container , Until all rows are scanned , Just return the answer directly .
3、 ... and 、 Problem solving
1、 Code implementation
class Solution {
public:
pair<int,int> minLine(vector<int>& nums,int length){
int min = 0x1f1f1f1f,index = 0;
for(int i = 0; i < length; i++){
if(nums[i] < min){
min = nums[i];
index = i;
}
}
return {min,index};
}
int maxRow(vector<vector<int>>& matrix,int rowLength,int rowNum){
int maxNum = 0;
for(int i = 0; i < rowLength; i++){
maxNum = max(maxNum,matrix[i][rowNum]);
}
return maxNum;
}
vector<int> luckyNumbers (vector<vector<int>>& matrix) {
// Save row element size 、 And column element size
int lineLength = matrix[0].size(), rowLength = matrix.size();
vector<int> ans;
for(auto& it : matrix){
// Scan a line , Get the minimum number of the row and its subscript
auto lineMinIndex = minLine(it,lineLength);
// Subscript according to the minimum number of rows , Get the column number , Scan the column Find the largest element of the column
int rowMax = maxRow(matrix, rowLength, lineMinIndex.second);
// contrast The minimum number of this line Whether it is The maximum number in the column
if(lineMinIndex.first == rowMax){
ans.push_back(rowMax);
}
}
return ans;
}
};
2、 Time complexity and Spatial complexity
Time complexity :
,m And n Respectively Number of row and column numbers
Spatial complexity :
边栏推荐
- 7 — filter
- DDoS attacks - are we really at war?
- dhu编程练习
- 9 - regular check set
- [graph neural network] overview of graph classification learning [2]: graph classification based on graph neural network
- 每周推荐短视频:为什么理论正确但得不到预期结果?
- [MySQL 05] SUSE 12 SP5 modifies the MySQL password for the first time after installing MySQL
- 8 — router
- JS reverse case -rus5 logic learning
- 006_ radio
猜你喜欢

Share the source code of the website of graduation student record

005_ button

Add a second network card (network interface NIC) for the virtual machine in azure portal in 2 minutes

Implementation of a simple camera based on pyqt5

搞透AQS原理(流程图及同步队列图解)

33Mysql

搞透AQS原理(流程圖及同步隊列圖解)

Weekly recommended short video: why is the theory correct but can not get the expected results?

每周推荐短视频:为什么理论正确但得不到预期结果?

JS reverse case -rus5 logic learning
随机推荐
Day_ 19 multithreading Basics
012_ switch
dhu编程练习
[MySQL 05] SUSE 12 SP5 modifies the MySQL password for the first time after installing MySQL
Matlab 2012a 绘制带箭头的线段
Weekly recommended short video: why is the theory correct but can not get the expected results?
018_ rate
Using grpcui to test asp Net core grpc service
Yyds dry inventory consistent and smooth zoom type and spacing
Internet Crime Complaint Center reports an increase in DDoS Attacks
33Mysql
搞透AQS原理(流程圖及同步隊列圖解)
If you want to install a set of monitoring, what is the process? How much is it?
Add a second network card (network interface NIC) for the virtual machine in azure portal in 2 minutes
Using face_ Recognition library reports an error reason: cudnn_ STATUS_ NOT_ SUPPORTED
Is the processor the main factor in buying a mobile phone?
What problems can cloud storage architecture solve for Devops?
【银河麒麟V10】【桌面】火狐浏览器设置主页不生效
What is idempotency? Detailed explanation of four interface idempotence schemes!
Comprendre le principe AQS (organigramme et schéma de file d'attente synchrone)