当前位置:网站首页>leetcode1337. 矩阵中战斗力最弱的K行
leetcode1337. 矩阵中战斗力最弱的K行
2022-06-21 21:43:00 【2021dragon】

LeetCode系列文章
一、题目描述
给你一个大小为 m × n m\times n m×n的矩阵 m a t mat mat,矩阵由若干军人和平民组成,分别用 1 1 1 和 0 0 0 表示。
请你返回矩阵中战斗力最弱的 k k k 行的索引,按从最弱到最强排序。
如果第 i i i 行的军人数量少于第 j j j 行,或者两行军人数量相同但 i i i 小于 j j j,那么我们认为第 i i i 行的战斗力比第 j j j 行弱。
军人总是排在一行中的靠前位置,也就是说 1 1 1 总是出现在 0 0 0 之前。
二、示例
输入: mat = [ [1, 1, 0, 0, 0 ],
[1, 1, 1, 1, 0 ],
[1, 0, 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]
三、主体思路
想要找出矩阵中战斗力最弱的K行,我们可以先将矩阵中的每行按战斗力的强弱进行排序,然后就可以选取出战斗力最弱的K行。
在排序的过程中需要将各行的战斗力进行比较,因此我们需要设置一个战斗力的比较方式。
- 根据题意可知,在比较战斗力时,需要先比较两行中1的个数。
- 若两行中1的个数不同,则1的个数较少的一行战斗力较弱。
- 若两行中1的个数相同,则还需要继续比较行号,行号较小的一行战斗力较弱。
按照该比较方式将各行按战斗力进行排序后,就能够选取出战斗力最弱的K行。
四、代码实现
在实现代码时,可以用pair来表示某行的战斗力。
- 将该行中1的个数作为pair当中first成员。
- 将该行的行号作为pair当中的second成员。
因为pair类型变量在比较时默认就是先比较first成员,如果first成员相同再比较second成员,这正好符号该题战斗力的比较规则,而排序的方式可以是任意的,这里以堆排序为例。
因此解题步骤如下:
- 遍历矩阵的各行,统计出每行中1的个数,构成
<1的个数,行号>的键值对。- 用各行的战斗力进行建堆(小堆)。
- 从堆顶取出战斗力最弱的k行。
代码如下:
说明一下:
- 因为每行中的1总是出现在0之前的,所以在统计某行中1的个数时可以采用二分查找的方式,找到该行中最后那个1的位置pos,此时pos+1对应的就是该行中1的个数。
- 在建堆时,最好不要将每行的pair一个个放入堆中,而应该直接用这若干个pair进行建堆,因为前者的时间复杂度是 O ( N l o g N ) O(NlogN) O(NlogN),而后者的时间复杂度是 O ( N ) O(N) O(N)。
边栏推荐
- uniapp版本更新 热更新及自然更新
- Software testing concepts
- 企业综合组网实训二
- Some users of uniapp wechat authorization cannot be authorized normally
- Produced by Ali! Graphical ant script - idea plug-in cloudtoolkit
- Getting started with reverse debugging - Basics
- 378. 有序矩阵中第 K 小的元素-常规法
- How to adjust the resolution of the computer screen? Computer screen modification resolution switchresx
- How to associate the QR code of wechat applet and realize the integration of two codes
- Hardware development notes (IV): basic process of hardware development, making a USB to RS232 module (III): design schematic diagram
猜你喜欢

花200W买流量,不如0成本起步做独立站私域运营收益高!

关于 麒麟系统开发错误“fatal error: GL/gl.h: No such file or directory“ 的解决方法

SIGIR2022 | 对话式推荐系统中的用户偏好建模

WSL 2 installation process (and introduction)

一家低代码厂商停服后……

SIGIR2022 | 對話式推薦系統中的用戶偏好建模

numpy矩阵初等变换

CISSP certification 2021 textbook OSG 9th Edition added (modified) knowledge points: comparison with the 8th Edition

关于 allegro的pcbEditor在使用过程中经常卡或者busy无响应 的解决方法

Software testing Q & A
随机推荐
硬件开发笔记(三):硬件开发基本流程,制作一个USB转RS232的模块(二):设计原理图库
微信小程序获取网络状态
1016. substring can represent binary string of numbers from 1 to n
Sigir2022 | modélisation des préférences des utilisateurs dans le système de recommandation dialogique
Getting to know Vxe table (I)
The solution to the error "xxx.pri has modification time XXXX s in the futrue" in the compilation of domestic Kirin QT
H5 wechat authorized login (wechat authorized login of the uniapp web version)
Qt中常用的窗体
Uniapp encapsulates the request function to achieve unique login. One account can only log in to one device at the same time
What are the trends of cloud computing in 2022?
uni-app进阶之样式框架/生产环境【day10】
Specific methods of using cloud development to realize wechat payment
在线文本按行批量反转工具
Wechat applet obtains network status
uniapp微信授权之 有个别用户 无法正常授权
An example of data format conversion in MySQL
Using JS function in wxml file of applet
Explain JS micro task and macro task in simple terms
有一说一,高并发系统设计其实一点都不难!
解决笔记本电脑(i)某个键的字母按不出来
