当前位置:网站首页>leetcode1337. Row K with the weakest combat effectiveness in the matrix
leetcode1337. Row K with the weakest combat effectiveness in the matrix
2022-06-21 23:37:00 【2021dragon】

LeetCode Series articles
List of articles
One 、 Title Description
Give you a size of m × n m\times n m×n Matrix m a t mat mat, The matrix consists of a number of soldiers and civilians , Use them separately 1 1 1 and 0 0 0 Express .
Please return to the weakest in the matrix k k k Index of rows , Sort by weakest to strongest .
If the first i i i The number of soldiers in line is less than the number of j j j That's ok , Or two lines of soldiers in the same number but i i i Less than j j j, So we think the i i i The battle effectiveness of the line is better than the first j j j Row weakness .
Soldiers are always at the top of the line , in other words 1 1 1 Always in 0 0 0 Before .
Two 、 Example
Input : mat = [ [1, 1, 0, 0, 0 ],
[1, 1, 1, 1, 0 ],
[1, 0, 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]
3、 ... and 、 Main idea
Want to find out the weakest battle effectiveness in the matrix K That's ok , We can sort each row in the matrix according to the strength of combat effectiveness , Then you can select the one with the weakest combat effectiveness K That's ok .
In the process of sorting, you need to compare the combat effectiveness of each row , So we need to set up a way to compare combat effectiveness .
- According to the meaning of the question , When comparing combat effectiveness , You need to compare the two lines first 1 The number of .
- If in two lines 1 The number of different , be 1 The battle effectiveness of the row with a small number of is weak .
- If in two lines 1 The same number of , You need to continue to compare line numbers , The line with smaller line size has weaker combat effectiveness .
After sorting each row by combat effectiveness according to this comparison method , We can select the one with the weakest combat effectiveness K That's ok .
Four 、 Code implementation
When implementing code , It can be used pair To show the combat effectiveness of a certain line .
- In this line 1 As the number of pair among first member .
- Take the line number of this line as pair In the middle of second member .
because pair Type variables are compared first by default first member , If first Compare the same members second member , This is just a symbol of the rules for comparison of combat effectiveness , The sorting method can be arbitrary , Here, take heap sorting as an example .
So the steps to solve the problem are as follows :
- Traverse the rows of the matrix , Count the... In each row 1 The number of , constitute
<1 The number of , Line number >The key/value pair .- Use the combat power of each line to build piles ( Little heap ).
- Take out the weakest weapon from the top of the pile k That's ok .
The code is as follows :
Explain :
- Because... In each line 1 Always in 0 Previous , So in a row of Statistics 1 The binary search method can be used to find the number of , Find the last one in the line 1 The location of pos, here pos+1 The corresponding is in this line 1 The number of .
- When building a reactor , It's best not to put each line of pair Put them in the heap one by one , Instead, you should use these pair Build the reactor , Because the time complexity of the former is O ( N l o g N ) O(NlogN) O(NlogN), The time complexity of the latter is O ( N ) O(N) O(N).
边栏推荐
- 211高校神级硕士论文刷屏!75行字错了20行!学校回应:导师停招...
- 开发环境和测试环境的发包(及uniapp的request封装)
- uniapp微信授权之 有个别用户 无法正常授权
- Solve the problem that the letter of a key in laptop (I) cannot be pressed
- Some users of uniapp wechat authorization cannot be authorized normally
- H5之微信授权登陆 (uniapp网页版微信授权登录)
- H5 wechat authorized login (wechat authorized login of the uniapp web version)
- Uniapp version update hot update and natural update
- Inventaire des exploits courants
- Hardware development notes (IV): basic process of hardware development, making a USB to RS232 module (III): design schematic diagram
猜你喜欢

硬件开发笔记(三):硬件开发基本流程,制作一个USB转RS232的模块(二):设计原理图库

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

#CISSP认证2021年教材 OSG 第9版 增(改)知识点:与 第8版 的目录对比

WSL 2 的安装过程(以及介绍)

uniapp版本更新 热更新及自然更新

硬件开发笔记(五):硬件开发基本流程,制作一个USB转RS232的模块(四):创建CON连接器件封装并关联原理图元器件

uniapp微信授权之 有个别用户 无法正常授权

Leetcode-543- diameter of binary tree

4. ESP8266通过OLED实时显示DHT11温湿度参数

RK3568开发笔记(二):入手RK3568开发板的套件介绍、底板介绍和外设测试
随机推荐
Software testing Q & A
leetcode1337. 矩阵中战斗力最弱的K行
Notes on the development of raspberry pie (15): Raspberry pie 4b+ compile and install MySQL database from the source code
What are the trends of cloud computing in 2022?
Solution to garbled Chinese display of securefx transmission remote server
How to use metric unit buffer in PostGIS
CISSP certification 2021 textbook OSG 9th Edition added (modified) knowledge points: comparison with the 8th Edition
Jmter test command [note]
Uniapp solves the cross domain problem of Google browser and runs in Google browser
Solutions to the problem that Allegro's pcbeditor is often stuck or busy in use
RK3568开发笔记(三):RK3568虚拟机基础环境搭建之更新源、安装网络工具、串口调试、网络连接、文件传输、安装vscode和samba共享服务
树莓派开发笔记(十七):树莓派4B+上Qt多用户连接操作Mysql数据库同步(单条数据悲观锁)
mongo 内存占用过大被系统自动关闭问题
Inventaire des exploits courants
Hardware development notes (V): basic process of hardware development, making a USB to RS232 module (IV): creating con connection device package and associating principle element devices
Some users of uniapp wechat authorization cannot be authorized normally
Pltmodify abscissa and ordinate colors
Sigir2022 | modélisation des préférences des utilisateurs dans le système de recommandation dialogique
Explain JS micro task and macro task in simple terms
JS implementation of Fibonacci sequence
