-
Notifications
You must be signed in to change notification settings - Fork 0
/
1337.the-k-weakest-rows-in-a-matrix.java
46 lines (36 loc) · 1.16 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
class Solution {
public int[] kWeakestRows(int[][] mat, int k) {
Clan[] clans = new Clan[mat.length];
for(int i=0;i<mat.length;i++){
int noOfSoliders= 0;
for(int j=0;j<mat[0].length && mat[i][j] >0;j++)
noOfSoliders++;
clans[i] = new Clan(i, noOfSoliders);
}
Arrays.sort(clans, new SortBySolidersComparator());
int[] res = new int[k];
for(int i=0;i<k;i++)
res[i] = clans[i].ind;
return res;
}
class Clan{
int ind;
int noOfSolider;
Clan(int ind, int noOfSolider){
this.ind = ind;
this.noOfSolider = noOfSolider;
}
@Override
public String toString(){
return this.noOfSolider+" "+this.ind;
}
}
class SortBySolidersComparator implements Comparator<Clan> {
@Override
public int compare(Clan c1, Clan c2){
if(c1.noOfSolider == c2.noOfSolider)
return c1.ind-c2.ind;
return c1.noOfSolider-c2.noOfSolider;
}
}
}