当前位置:网站首页>【力扣】1337.矩阵中战斗力最弱的k行
【力扣】1337.矩阵中战斗力最弱的k行
2022-07-28 03:29:00 【aigo-2021】
给你一个大小为 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
代码:
class Solution {
public int[] kWeakestRows(int[][] mat, int k) {
TreeMap<Integer, Integer> map = new TreeMap<>();//有序集合
for (int i = 0; i < mat.length; i++) {
int key = i;
for (int j = 0; j < mat[i].length; j++) {
if (mat[i][j] == 0) {
break;
}
key += mat[i][j] * 100; //200,401,102,203,504
}
map.put(key, i);
}
int[] arr = new int[k];
int index = 0;
for (Integer key1 : map.keySet()) {//map.keySet()返回map中的key值
//表示将map对象的所有key值已set集合的形式返回,因为map也是无序的,且key值也是不可重复的,因此这里用set集合存储key并返回也符合规则。
if (index == k) break;//循环结束的条件
arr[index++] = map.get(key1);//通过key值获取map中的value,存储到arr数组中
}
return arr;
}
public static void main(String[] args) {
Solution s = new Solution();
int[][] arr = new int[][]{
{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}};
System.out.println(Arrays.toString(s.kWeakestRows(arr, 3)));
}
}

边栏推荐
- Msgan is used for pattern search of multiple image synthesis to generate confrontation Network -- to solve the problem of pattern collapse
- 53. Maximum subarray maximum subarray sum
- Daily practice ----- realize the lottery function of two-color ball. Rules: Six non repeating numbers are randomly selected from 36 red balls, and one from 15 basketball is randomly selected to form a
- Airiot Q & A issue 6 | how to use the secondary development engine?
- Xctf attack and defense world web master advanced area php2
- Redis basic operation
- 贪心——53. 最大子数组和
- Responsive high-end website template source code Gallery material resource download platform source code
- Asemi rectifier bridge gbpc5010, gbpc5010 parameters, gbpc5010 size
- 光年(Light Year Admin)后台管理系统模板
猜你喜欢

单调栈——42. 接雨水——面大厂必须会的困难题

695. 岛屿的最大面积

TypeError: ufunc ‘bitwise_and‘ not supported for the input types, and the inputs could not be safely

鼠标操作和响应

接口自动化测试,完整入门篇

Daily practice ----- realize the lottery function of two-color ball. Rules: Six non repeating numbers are randomly selected from 36 red balls, and one from 15 basketball is randomly selected to form a

VMware virtual machine network settings

C language to achieve a dynamic version of the address book

"Xiaodeng" network equipment monitoring in operation and maintenance

收藏|0 基础开源数据可视化平台 FlyFish 大屏开发指南
随机推荐
ThreadLocal usage scenario
2022最新Android Handler相关面试题总结
Embedded database -- SQLite
【OPENVX】对象基本使用之vx_distribution
单调栈——739. 每日温度
「运维有小邓」网络设备监控
如何让外网访问内网IP(esp8266网页使用)
A treasure simulates login and reduces the method of secondary verification
The open source of "avoiding disease and avoiding medicine" will not go far
玩一玩WolframAlpha计算知识引擎
Outlook 教程,如何在 Outlook 中使用颜色类别和提醒?
20 soul chicken soup beautiful sentences, sentence by sentence warm heart!
4-day excel practical training camp, 0.01 yuan special offer for only three days, 200 sets of learning kits
【OPENVX】对象基本使用之vx_image
SQL Server备份数据库的方法
某宝模拟登录,减少二次验证的方法
同时导出多个excel,并且一个excel中包含多个sheet
Redis source code analysis (who says C language can't analyze it?)
Light year admin background management system template
Redis persistence mechanism