forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1341.java
31 lines (29 loc) · 949 Bytes
/
_1341.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class _1341 {
public static class Solution1 {
public int[] kWeakestRows(int[][] mat, int k) {
List<int[]> list = new ArrayList<>();
for (int i = 0; i < mat.length; i++) {
int soldiers = 0;
for (int j = 0; j < mat[0].length; j++) {
if (mat[i][j] == 1) {
soldiers++;
} else {
break;
}
}
list.add(new int[]{i, soldiers});
}
Collections.sort(list, (a, b) -> a[1] == b[1] ? a[0] - b[0] : a[1] - b[1]);
int[] result = new int[k];
int i = 0;
while (i < k) {
result[i] = list.get(i++)[0];
}
return result;
}
}
}