当前位置:网站首页>Leetcode 1337. Row K with the weakest combat effectiveness in the matrix
Leetcode 1337. Row K with the weakest combat effectiveness in the matrix
2022-04-23 20:26:00 【Die in a trap】
1337、 The weakest in the matrix K That's ok
1) Title Description
Give you a size of m * n Matrix mat, The matrix consists of a number of soldiers and civilians , Use them separately 1 and 0 Express .
Please return to the weakest in the matrix k Index of rows , Sort by weakest to strongest .
If the first i The number of soldiers in line is less than the number of j That's ok , Or two lines of soldiers in the same number but i Less than j, So we think the i The battle effectiveness of the line is better than the first j Row weakness .
Soldiers Always The front position in a row , in other words 1 Always in 0 Before .
Example 1:
Input :mat =
[[1,1,0,0,0],
[1,1,1,1,0],
[1,0,0,0,0],
[1,1,0,0,0],
[1,1,1,1,1]],
k = 3
Output :[2,0,3]
explain :
The number of soldiers in each line :
That's ok 0 -> 2
That's ok 1 -> 4
That's ok 2 -> 1
That's ok 3 -> 2
That's ok 4 -> 5
Sort these rows from the weakest to the strongest to get [2,0,3,1,4]
Example 2:
Input :mat =
[[1,0,0,0],
[1,1,1,1],
[1,0,0,0],
[1,0,0,0]],
k = 2
Output :[0,2]
explain :
The number of soldiers in each line :
That's ok 0 -> 1
That's ok 1 -> 4
That's ok 2 -> 1
That's ok 3 -> 1
Sort these rows from the weakest to the strongest to get [0,2,3,1]
Tips :
m == mat.lengthn == mat[i].length2 <= n, m <= 1001 <= k <= mmatrix[i][j]No 0 Namely 1
2) analysis
- Find the last... In each line by bisection
1The subscript , To calculate the combat effectiveness of each line , Gain combat effectiveness array ; - Assign the combat effectiveness of each row to the temporary array , And sort it out ;
- Through violent comparison , Find the first... Of the temporary array
kThe subscript of a value in the combat effectiveness array .
3)C++ Code
class Solution {
public:
vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
vector<int> res;
for(int i=0;i<mat.size();i++){
int left=0;
int right=mat[0].size()-1;
while(left<=right){
int mid=(left+right)/2;
if(!mat[i][mid])
right=mid-1;
else
left=mid+1;
}
res.push_back(left+1);
}
vector<int>temp=res;
sort(temp.begin(),temp.end());
vector<int> ans;
for(int i=0;i<k;i++){
for(int j=0;j<res.size();j++){
if(res[j]==temp[i]){
ans.push_back(j);
res[j]=INT_MAX;
break;
}
}
}
return ans;
}
};
版权声明
本文为[Die in a trap]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204232023107660.html
边栏推荐
- How do BIM swindlers cheat? (turn)
- Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
- go-zero框架数据库方面避坑指南
- Monte Carlo py solves the area problem! (save pupils Series)
- The R language uses the timeroc package to calculate the multi time AUC value of survival data without competitive risk, and uses the confint function to calculate the confidence interval value of mul
- Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 127
- PCA based geometric feature calculation of PCL point cloud processing (52)
- SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
- PCL点云处理之直线与平面的交点计算(五十三)
- [graph theory brush question-4] force deduction 778 Swimming in a rising pool
猜你喜欢

The construction and use of Fortress machine and springboard machine jumpserver are detailed in pictures and texts

Shanghai a répondu que « le site officiel de la farine est illégal »: l'exploitation et l'entretien négligents ont été « noirs » et la police a déposé une plainte

Numpy mathematical function & logical function

波场DAO新物种下场,USDD如何破局稳定币市场?

考研英语唐叔的语法课笔记
![[talkative cloud native] load balancing - the passenger flow of small restaurants has increased](/img/ba/4ccf0c2181572fed16bbc9c797d557.png)
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased

Notes of Tang Shu's grammar class in postgraduate entrance examination English

堡垒机、跳板机JumpServer的搭建,以及使用,图文详细

Customize timeline component styles

Modeling based on catiav6
随机推荐
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
LeetCode 542、01 矩阵
XXXI` Prototype ` displays prototype properties and`__ proto__` Implicit prototype properties
Scrapy教程 - (2)寫一個簡單爬蟲
How do BIM swindlers cheat? (turn)
PCA based geometric feature calculation of PCL point cloud processing (52)
Solve the Chinese garbled code of URL in JS - decoding
Introduction to link database function of cadence OrCAD capture CIS replacement components, graphic tutorial and video demonstration
The second method of file upload in form form is implemented by fileitem class, servletfileupload class and diskfileitemfactory class.
[problem solving] 'ASCII' codec can't encode characters in position XX XX: ordinal not in range (128)
Intersection calculation of straight line and plane in PCL point cloud processing (53)
The R language uses the timeroc package to calculate the multi time AUC value of survival data without competitive risk, and uses the confint function to calculate the confidence interval value of mul
Cadence OrCAD capture batch change component packaging function introduction graphic tutorial and video demonstration
WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
Servlet learning notes
Mysql database and table building: the difference between utf8 and utf8mb4
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
Thirty What are VM and VC?
R language uses the preprocess function of caret package for data preprocessing: BoxCox transform all data columns (convert non normal distribution data columns to normal distribution data and can not
CVPR 2022 | querydet: use cascaded sparse query to accelerate small target detection under high resolution