当前位置:网站首页>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
边栏推荐
- [graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph
- Cadence Orcad Capture 批量更改元件封装功能介绍图文教程及视频演示
- Latest investigation and progress of building intelligence based on sati
- An error is reported in the initialization metadata of the dolphin scheduler -- it turns out that there is a special symbol in the password. "$“
- Recognition of high-speed road signs by Matlab using alexnet
- Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]
- bounding box iou
- 黑客的入侵方式你知道几种?
- Markdown < a > tag new page open link
- Wave field Dao new species end up, how does usdd break the situation and stabilize the currency market?
猜你喜欢

Matlab analytic hierarchy process to quickly calculate the weight
ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics

BMP JPEG 图片转换为矢量图像 ContourTrace

Automatically fill in body temperature and win10 task plan

A useless confession artifact

How can matlab obtain the truncated image in trainingimagelabeler

Imitation Baidu map realizes the three buttons to switch the map mode by automatically shrinking the bottom
![[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
![[stack and queue topics] - sliding window](/img/65/a2ce87f1401d7a28d4cce0cc85175f.png)
[stack and queue topics] - sliding window

Go zero framework database avoidance Guide
随机推荐
PostgreSQL basic functions
[graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph
Livego + ffmpeg + RTMP + flvjs to realize live video
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
LeetCode 232、用栈实现队列
16MySQL之DCL 中 COMMIT和ROllBACK
Installation and use of NVM
Confusion about thread blocking after calling the read () method of wrapper flow
SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in
论文写作 19: 会议论文与期刊论文的区别
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
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
The ODB model calculates the data and outputs it to excel
The second method of file upload in form form is implemented by fileitem class, servletfileupload class and diskfileitemfactory class.
Numpy sort search count set
Computing the intersection of two planes in PCL point cloud processing (51)
JDBC tool class jdbcfiledateutil uploads files and date format conversion, including the latest, simplest and easiest way to upload single files and multiple files
【问题解决】‘ascii‘ codec can‘t encode characters in position xx-xx: ordinal not in range(128)
Historical track data reading of Holux m1200-e Bluetooth GPS track recorder
R language uses timeroc package to calculate the multi time AUC value of survival data under competitive risk, uses Cox model and adds covariates, and R language uses the plotauccurve function of time