当前位置:网站首页>Algorithm - the K row with the weakest combat power in the matrix (kotlin)
Algorithm - the K row with the weakest combat power in the matrix (kotlin)
2022-06-24 09:37:00 【Xiaomi technology Android R & D caoxinyu】
subject
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
source : Power button (LeetCode)
link :https://leetcode.cn/problems/the-k-weakest-rows-in-a-matrix
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
resolvent
fun kWeakestRows(mat: Array<IntArray>, k: Int): IntArray {
var pair = Array(mat.size) {
Array(2) {
0 } }
mat.forEachIndexed {
index, ints ->
run {
pair[index][0] = index
mat[index].forEach {
pair[index][1] += it
}
}
}
Arrays.sort(pair,0,pair.size) {
o1, o2 -> if (o1[1] > o2[1]) 1 else if (o1[1] == o2[1]) 0 else -1 }
val result = IntArray(k) {
0 }
for (i in 0 until k){
result[i] = pair[i][0]
}
return result
}
summary
1. Because soldiers are always ahead , So you can also index Judge the combat effectiveness of the current row , Dichotomy is faster to find the maximum combat effectiveness , Not all of them
边栏推荐
- threejs的点光源+环境光
- 二十、处理器调度(RR时间片轮转,MLFQ多级反馈队列,CFS完全公平调度器,优先级翻转;多处理器调度)
- The printed object is [object object]. Solution
- 每周推荐短视频:谈论“元宇宙”要有严肃认真的态度
- Numpy NP in numpy c_ And np r_ Explain in detail
- 算法---矩阵中战斗力最弱的 K 行(Kotlin)
- 带文字的seekbar : 自定义progressDrawable/thumb :解决显示不全
- LeetCode: 240. 搜索二维矩阵 II
- Leetcode -- linked list
- Support vector machine (SVC, nusvc, linearsvc)
猜你喜欢
随机推荐
Codeforces Round #392 (Div. 2) D. Ability To Convert
每周推荐短视频:谈论“元宇宙”要有严肃认真的态度
WindowManager 简单悬浮框的实现
Target of cmake command_ compile_ options
数字化转型的失败原因及成功之道
Depens:*** but it is not going to be installed
PTA猴子选大王(约瑟夫环问题)
Oracle数据库监听文件配置
每周推薦短視頻:談論“元宇宙”要有嚴肅認真的態度
【Eureka 源码分析】
Time Series Data Augmentation for Deep Learning: A Survey 之论文阅读
Webrtc series - network transmission 5: select the optimal connection switching
198. 打家劫舍
P6698-[balticoi 2020 day2] virus [AC automata, DP, SPFA]
The ambition of JD instant retailing from 618
PRCT-1400 : 未能执行 getcrshome解决方法
ggplot2颜色设置总结
【Eureka注册中心】
2021-05-20computed and watch applications and differences
LeetCode: 137. 只出现一次的数字 II









