当前位置:网站首页>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.length
n == mat[i].length
2 <= n, m <= 100
1 <= k <= m
matrix[i][j]
No 0 Namely 1
2) analysis
- Find the last... In each line by bisection
1
The 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
k
The 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)
- PCA based geometric feature calculation of PCL point cloud processing (52)
- How about CICC fortune? Is it safe to open an account
- BMP JPEG picture to vector image contourtrace
- SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
- Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
- Handwritten Google's first generation distributed computing framework MapReduce
- DNS cloud school | analysis of hidden tunnel attacks in the hidden corner of DNS
- Commit and ROLLBACK in DCL of 16mysql
- SQL gets the latest record of the data table
猜你喜欢
Scrapy教程 - (2)寫一個簡單爬蟲
The ODB model calculates the data and outputs it to excel
Some basic configurations in interlij idea
考研英语唐叔的语法课笔记
Three. Based on ply format point cloud voxel model JS upload interface writing
Browser - learning notes
Plato farm is one of the four largest online IEOS in metauniverse, and the transaction on the chain is quite high
Commit and rollback in DCL of 16 MySQL
PCL点云处理之计算两平面交线(五十一)
Servlet learning notes
随机推荐
Scripy tutorial - (2) write a simple crawler
波场DAO新物种下场,USDD如何破局稳定币市场?
selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
Modeling based on catiav6
BMP JPEG 图片转换为矢量图像 ContourTrace
SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
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
Devaxpress report replay: complete the drawing of conventional two-dimensional report + histogram + pie chart
How can matlab obtain the truncated image in trainingimagelabeler
How about CICC fortune? Is it safe to open an account
2022 - Data Warehouse - [time dimension table] - year, week and holiday
Mysql database and table building: the difference between utf8 and utf8mb4
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
Still using listview? Use animatedlist to make list elements move
Browser - learning notes
Imitation Baidu map realizes the three buttons to switch the map mode by automatically shrinking the bottom
[problem solving] 'ASCII' codec can't encode characters in position XX XX: ordinal not in range (128)
Experience of mathematical modeling in 18 year research competition
Cadence Orcad Capture CIS更换元器件之Link Database 功能介绍图文教程及视频演示
What is the difference between a host and a server?