-
Notifications
You must be signed in to change notification settings - Fork 0
/
1337. The K Weakest Rows in a Matrix.java
80 lines (66 loc) · 1.58 KB
/
1337. The K Weakest Rows in a Matrix.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//Using Priority Queue
class Solution {
public int[] kWeakestRows(int[][] mat, int k) {
PriorityQueue<int[]> min_heap = new PriorityQueue<>((x, y) -> {
if(x[0] != y[0]) {
return Integer.compare(x[0], y[0]);
}
if(x[1] != y[1]) {
return Integer.compare(x[1], y[1]);
}
return 0;
});
int row=mat.length;
int col=mat[0].length;
for(int i=0;i<row;i++){
int val=0;
for(int j=0;j<col;j++){
if(mat[i][j]==1)val++;
}
min_heap.add(new int[]{val,i});
}
int ans[]=new int[k];
int ind=0;
while(k>0){
int cur[]=min_heap.peek();
min_heap.poll();
ans[ind]=cur[1];
ind++;
k--;
}
return ans;
}
}
//Using List-List
class Solution {
public int[] kWeakestRows(int[][] mat, int k) {
List<List<Integer>>val_row=new ArrayList<>();
int row=mat.length;
int col=mat[0].length;
for(int i=0;i<=col;i++){
val_row.add(new ArrayList<>());
}
for(int i=0;i<row;i++){
int val=0;
for(int j=0;j<col;j++){
if(mat[i][j]==1)val++;
}
val_row.get(val).add(i);
}
int ans[]=new int[k];
int ind=0;
for(int i=0;i<=col;i++){
List<Integer>cur=val_row.get(i);
if(cur.size()!=0){
for(int j=0;j<cur.size() && k>0;j++){
ans[ind]=cur.get(j);
ind++;
k--;
// if(k==0)
// break;
}
}
}
return ans;
}
}