当前位置:网站首页>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.lengthn == mat[i].length2 <= n, m <= 1001 <= k <= mmatrix[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
边栏推荐
- Some basic knowledge of devexpress report development
- Common form verification
- 上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
- 2022 - Data Warehouse - [time dimension table] - year, week and holiday
- Still using listview? Use animatedlist to make list elements move
- 中金财富公司怎么样,开户安全吗
- Thirty What are VM and VC?
- BMP JPEG 图片转换为矢量图像 ContourTrace
- Tensorflow 2 basic operation dictionary
- Monte Carlo py solves the area problem! (save pupils Series)
猜你喜欢

go-zero框架数据库方面避坑指南

Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report

Click an EL checkbox to select all questions

Servlet learning notes

Recognition of high-speed road signs by Matlab using alexnet

An error is reported when sqoop imports data from Mysql to HDFS: sqlexception in nextkeyvalue

After route link navigation, the sub page does not display the navigation style problem

上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案

On BIM data redundancy theory

. Ren -- the intimate artifact in the field of vertical Recruitment!
随机推荐
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
三十一. `prototype`显示原型属性和`__proto__`隐式原型属性
Scrapy教程 - (2)寫一個簡單爬蟲
【问题解决】‘ascii‘ codec can‘t encode characters in position xx-xx: ordinal not in range(128)
RT-1052学习笔记 - GPIO架构分析
On BIM data redundancy theory
Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)
Intersection calculation of straight line and plane in PCL point cloud processing (53)
PCL点云处理之基于PCA的几何形状特征计算(五十二)
Es error: request contains unrecognized parameter [ignore_throttled]
R language ggplot2 visual facet_wrap, and use the lineheight parameter to customize the height of the facet icon tab (gray label bar)
2022 - Data Warehouse - [time dimension table] - year, week and holiday
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
How to protect ECs from hacker attacks?
SQL gets the latest record of the data table
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
bounding box iou
Mysql database and table building: the difference between utf8 and utf8mb4
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference