当前位置:网站首页>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 :
边栏推荐
- How to create a CSR (certificate signing request) file?
- [Galaxy Kirin V10] [desktop] Firefox browser settings home page does not take effect
- Openlayers 3 built in interaction
- Encapsulate a complete version of the uniapp image and video upload component, which can be used immediately, switch between images and videos, customize the upload button style, delete the button sty
- 【MySQL 06】linux + Docker容器环境中备份和还原MySQL数据库
- 快速排序
- DDoS extortion attacks: what you need to know
- Looking for thesaurus data [closed]
- 207. curriculum - graph theory, depth traversal
- NCA: the nine year old has launched a DDoS attack
猜你喜欢

图解 Google V8 # 19 :异步编程(二):V8 是如何实现 async/await 的?

What is idempotency? Detailed explanation of four interface idempotence schemes!

8 — router

004_ icon

After the blueprint node of ue5 is copied to UE4, all connections and attribute values are lost

桶排序

widget使用setImageViewBitmap方法设置bug分析
![[MySQL 04] use MySQL workbench 8.0 CE to back up and restore MySQL databases in Linux](/img/e7/fc2925a10ac5fb370dd221c3f4a46a.png)
[MySQL 04] use MySQL workbench 8.0 CE to back up and restore MySQL databases in Linux

Using face_ Recognition library reports an error reason: cudnn_ STATUS_ NOT_ SUPPORTED

Tencent released the first Office Photo 23 years ago. It's so chronological
随机推荐
dhu编程练习
dhu编程练习
[pytorch actual combat] generate confrontation network Gan: generate cartoon character avatars
Is the processor the main factor in buying a mobile phone?
Weekly recommended short video: why is the theory correct but can not get the expected results?
Copy entire directory to output folder maintain folder structure- Copy entire directory to output folder maintaining the folder structure?
210. Schedule II - depth traversal
How to create a CSR (certificate signing request) file?
DHU programming exercise
Implement vs to run only one source file at a time
How does payment splitting help B2B bulk commodity transactions?
33Mysql
记录生产的一次OOM异常
208. implement trie (prefix tree) - attach detailed notes
After the blueprint node of ue5 is copied to UE4, all connections and attribute values are lost
Widget uses setimageviewbitmap method to set bug analysis
DTW learning (dynamic time warping) -- Thought and code implementation
Internet Crime Complaint Center reports an increase in DDoS Attacks
Restore a 35k-55k Tencent Android Senior Engineer Interview
AI landing manufacturing: intelligent robots should have these four abilities