当前位置:网站首页>算法---矩阵中战斗力最弱的 K 行(Kotlin)
算法---矩阵中战斗力最弱的 K 行(Kotlin)
2022-06-24 08:21:00 【小米科技Android 研发曹新雨】
题目
给你一个大小为 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
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/the-k-weakest-rows-in-a-matrix
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解决方法
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
}
总结
1.因为军人始终在前面,所以还可以根据index 判断当前行的战斗力,二分法更快的找到最大的战斗力,而不是全部都遍历
边栏推荐
- R ellipse random point generation and drawing
- 当程序员被问会不会修电脑时… | 每日趣闻
- Zero foundation self-study SQL course | sub query
- Get post: do you really know the difference between requests??????
- Lu Qi: I am most optimistic about these four major technology trends
- L01_ How is an SQL query executed?
- 正则匹配手机号
- Jincang KFS replicator installation (oracle-kes)
- 2022.6.13-6.19 AI行业周刊(第102期):职业发展
- Code written by mysql, data addition, deletion, query and modification, etc
猜你喜欢

e的lnx为什么等于x

Cdga | how can we do well in data governance?

Zero foundation self-study SQL course | related sub query
![The printed object is [object object]. Solution](/img/fc/9199e26b827a1c6304fcd250f2301e.png)
The printed object is [object object]. Solution

CF566E-Restoring Map【bitset】

The border problem after the focus of input

关于thinkphp5 使用模型save()更新数据提示 method not exist:think\db\Query-&gt; 报错解决方案

零基础自学SQL课程 | 相关子查询

Time Series Data Augmentation for Deep Learning: A Survey 之论文阅读

Some common pitfalls in getting started with jupyter:
随机推荐
jupyter入门常见的几个坑:
零基础自学SQL课程 | 相关子查询
支持向量机(SVC,NuSVC,LinearSVC)
Get post: do you really know the difference between requests??????
Oracle 12c升级至19c后ORA-28000错误
获取带参数的微信小程序二维码-以及修改二维码LOGO源码分享
threejs的 mmd模型加载+轮廓加载+动画加载+音频加载+相机动画加载+ammojs加载 gltf模型的加载 +gltf的反射调整
2022.06.23 (traversal of lc_144,94145\
Oracle数据库监听文件配置
PHP使用递归和非递归方式实现创建多级文件夹
Weekly recommended short video: talk about "meta universe" with a serious attitude
CF566E-Restoring Map【bitset】
ggplot2颜色设置总结
Event registration Apache pulsar x kubesphere online meetup hot registration
Oracle数据文件头SCN不一致处理方法
The ambition of JD instant retailing from 618
Tools
Chapter 7 operation bit and bit string (III)
Directly applicable go coding specification
Yolox backbone -- implementation of cspparknet