当前位置:网站首页>【力扣】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)));
}
}

边栏推荐
- VMware虚拟机网络设置
- [SAML SSO solution] Shanghai daoning brings you SAML for asp NET/SAML for ASP. Net core download, trial, tutorial
- What is a virtual function?
- Color recognition method and exploration based on MATLAB
- CF 7月25日-7月31日做题记录
- How to solve the problem of win11 black desktop background?
- 如何解决mysql深分页问题
- MangoPapa 的实用小脚本(目录篇)
- Leaf recognition, color feature extraction, defect detection, etc
- Embedded database -- SQLite
猜你喜欢

Redis basic operation

Collection | 0 basic open source data visualization platform flyfish large screen development guide

每周推荐短视频:如何正确理解“精益”这个词?

SSM integration (integrated configuration)

如何解决mysql深分页问题

Robot development -- lead screw and guide rail

MySQL的碎片有哪些

ASEMI整流桥GBPC5010,GBPC5010参数,GBPC5010大小

IronOCR for .NET 2022.8

MySQL事务的ACID特性及并发问题实例分析
随机推荐
C language to achieve a dynamic version of the address book
Shell: one click deployment PXE
Practice of online problem feedback module (16): realize the function of checking details
整合SSM实现增删改查搜索
某宝模拟登录,减少二次验证的方法
STM32 RT-Thread虚拟文件系统挂载操作
响应式高端网站模板源码图库素材资源下载平台源码
[5g NR] RRC reject analysis
Outlook tutorial, how to use color categories and reminders in outlook?
12月份PMP考试首次采用新考纲,该怎么学?
SAP UI5 FileUploader 控件深入介绍 - 为什么需要一个隐藏的 iframe 试读版
What is a virtual function?
Light year admin background management system template
AI首席架构师12-AICA-百度OCR垂类规模化落地实践
xctf攻防世界 Web高手进阶区 unserialize3
How to make the Internet access the intranet IP (used by esp8266 web pages)
695. Maximum area of the island
2022-07-27:小红拿到了一个长度为N的数组arr,她准备只进行一次修改, 可以将数组中任意一个数arr[i],修改为不大于P的正数(修改后的数必须和原数不同), 并使得所有数之和为X的倍数。
机器人开发--丝杠与导轨
VMware virtual machine network settings