-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
37 lines (31 loc) · 1.04 KB
/
Main.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
32
33
34
35
36
37
package com.company;
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args) {
int[][] mat = new int[][]{
new int[]{1, 1, 1, 1, 1},
new int[]{1, 0, 0, 0, 0},
new int[]{1, 1, 0, 0, 0},
new int[]{1, 1, 1, 1, 0},
new int[]{1, 1, 1, 1, 1},
};
int k = 3;
kWeakestRows(mat, 3);
}
public static int[] kWeakestRows(int[][] mat, int k) {
PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
for (int i = 0; i < mat.length; i++) {
int li = 0, hi = mat[i].length - 1;
while (li <= hi) {
int mid = li + (hi - li) / 2;
if (mat[i][mid] == 0) hi = mid - 1;
else li = mid + 1;
}
heap.offer(new int[]{li, i});
}
int[] res = new int[k];
for (int i = 0; i < res.length; i++)
res[i] = heap.poll()[1];
return res;
}
}