当前位置:网站首页>LeetCode 1337、矩阵中战斗力最弱的 K 行
LeetCode 1337、矩阵中战斗力最弱的 K 行
2022-04-23 20:23:00 【亡于灬】
1337、矩阵中战斗力最弱的 K 行
1)题目描述
给你一个大小为 m * n
的矩阵 mat
,矩阵由若干军人和平民组成,分别用 1 和 0 表示。
请你返回矩阵中战斗力最弱的 k
行的索引,按从最弱到最强排序。
如果第 i 行的军人数量少于第 j 行,或者两行军人数量相同但 i 小于 j,那么我们认为第 i 行的战斗力比第 j 行弱。
军人 总是 排在一行中的靠前位置,也就是说 1 总是出现在 0 之前。
示例 1:
输入: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
输出:[2,0,3]
解释:
每行中的军人数目:
行 0 -> 2
行 1 -> 4
行 2 -> 1
行 3 -> 2
行 4 -> 5
从最弱到最强对这些行排序后得到 [2,0,3,1,4]
示例 2:
输入:mat =
[[1,0,0,0],
[1,1,1,1],
[1,0,0,0],
[1,0,0,0]],
k = 2
输出:[0,2]
解释:
每行中的军人数目:
行 0 -> 1
行 1 -> 4
行 2 -> 1
行 3 -> 1
从最弱到最强对这些行排序后得到 [0,2,3,1]
提示:
m == mat.length
n == mat[i].length
2 <= n, m <= 100
1 <= k <= m
matrix[i][j]
不是 0 就是 1
2)分析
- 通过二分查找每一行最后一个
1
的下标,来计算每一行的战斗力,获得战斗力数组; - 将每一行的战斗力赋值给临时数组,并将其排序;
- 通过暴力比对,找出临时数组的前
k
个值在战斗力数组中的下标。
3)C++
代码
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;
}
};
版权声明
本文为[亡于灬]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38342510/article/details/124360336
边栏推荐
- [stack and queue topics] - sliding window
- Identification of bolt points in aerial photography based on perception
- Why does ES6 need to introduce map when JS already has object type
- Mysql database backup scheme
- How do BIM swindlers cheat? (turn)
- [talkative cloud native] load balancing - the passenger flow of small restaurants has increased
- DNS cloud school rising posture! Three advanced uses of authoritative DNS
- [PTA] get rid of singles
- Confusion about thread blocking after calling the read () method of wrapper flow
- Click an EL checkbox to select all questions
猜你喜欢
Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
Installation and use of NVM
Redis cache penetration, cache breakdown, cache avalanche
Plato Farm元宇宙IEO上线四大,链上交易颇高
Monte Carlo py solves the area problem! (save pupils Series)
Three. Based on ply format point cloud voxel model JS upload interface writing
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
Rt-1052 learning notes - GPIO architecture analysis
Servlet learning notes
Operation of numpy array
随机推荐
DTMF双音多频信号仿真演示系统
Numpy sort search count set
A useless confession artifact
JDBC database addition, deletion, query and modification tool class
Confusion about thread blocking after calling the read () method of wrapper flow
ABAQUS script email auto notification
Mysql database and table building: the difference between utf8 and utf8mb4
Rédaction de thèses 19: différences entre les thèses de conférence et les thèses périodiques
网络通信基础(局域网、广域网、IP地址、端口号、协议、封装、分用)
Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)
三十一. `prototype`显示原型属性和`__proto__`隐式原型属性
PCL点云处理之直线与平面的交点计算(五十三)
Linux64Bit下安装MySQL5.6-不能修改root密码
go-zero框架数据库方面避坑指南
bounding box iou
Thirty What are VM and VC?
Matlab analytic hierarchy process to quickly calculate the weight
On BIM data redundancy theory
【栈和队列专题】—— 滑动窗口