当前位置:网站首页>[force deduction] 1337. Row K with the weakest combat effectiveness in the matrix
[force deduction] 1337. Row K with the weakest combat effectiveness in the matrix
2022-07-28 03:38:00 【aigo-2021】
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 , perhaps The number of soldiers in the two lines is the same, 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
Code :
class Solution {
public int[] kWeakestRows(int[][] mat, int k) {
TreeMap<Integer, Integer> map = new TreeMap<>();// Ordered set
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() return map Medium key value
// It means that you will map All of the objects key Value already set The form of the collection returns , because map It's also out of order , And key Values are also non repeatable , So here we use set Collective storage key And the return also conforms to the rules .
if (index == k) break;// Conditions at the end of the cycle
arr[index++] = map.get(key1);// adopt key Values obtained map Medium value, Store in arr Array
}
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)));
}
}

边栏推荐
- Golang gets the tag of the loop nested structure
- 【OPENVX】对象基本使用之vx_matrix
- Shell: one click deployment PXE
- In December, the PMP Exam adopted the new syllabus for the first time. How to learn?
- Use of custom annotations
- 接口自动化测试,完整入门篇
- 【OPENVX】对象基本使用之vx_image
- How to use JDBC to operate database
- Redis communication protocol -- resp protocol
- 沃尔沃:深入人心的“安全感” 究竟靠的是什么?
猜你喜欢

每日练习------实现双色球的彩票功能。规则:从36个红球中随机选择不重复的6个数,从15个篮球中随机选择1个组成一注彩票。可以选择买多注。

SAP UI5 FileUploader 控件深入介绍 - 为什么需要一个隐藏的 iframe 试读版

AIRIOT答疑第6期|如何使用二次开发引擎?

最新版宝塔安装zip扩展,php -m 不显示的处理方法

Illustrated: detailed explanation of JVM memory layout

2022 summary of the latest Android handler related interview questions

How to arrange PCB screen printing? Please check this manual!

20220726汇承科技的蓝牙模块HC-05的AT命令测试

verticle-align行内元素垂直居中对齐

【力扣】1337.矩阵中战斗力最弱的k行
随机推荐
ASEMI整流桥GBPC3510在直流有刷电机中的妙用
AI chief architect 12 AICA Baidu OCR vertical large-scale landing practice
我的创作纪念日
什么是Tor?Tor浏览器更新有什么用?
服务器内存故障预测居然可以这样做!
Use of custom annotations
Server memory failure prediction can actually do this!
Animation
Assembly method of golang Gorm query arbitrary fields
动态规划——474. 一和零
TypeError: ufunc ‘bitwise_and‘ not supported for the input types, and the inputs could not be safely
某宝模拟登录,减少二次验证的方法
Tensorboard usage record
Golang gets the tag of the loop nested structure
【OPENVX】对象基本使用之vx_distribution
20220726 at command test of Bluetooth module hc-05 of Huicheng Technology
golang gorm查询任意字段的组装方法
Response to questions about the balanced beacon group of Hubei University of Arts and Sciences
C语言实现动态版本的通讯录
What is tor? What is the use of tor browser update?