|
1 | 1 | classSolution {
|
2 |
| -publicint[]kWeakestRows(int[][]mat,intk) { |
3 |
| -PriorityQueue<int[]>pq =newPriorityQueue<>( |
4 |
| -Comparator.comparingInt((int[]o) ->o[1]).thenComparingInt(o ->o[0])); |
5 |
| -for (inti =0;i <mat.length;i++) { |
6 |
| -pq.add(newint[]{i,getNumberOfOnes(mat[i])}); |
| 2 | +publicint[]kWeakestRows(int[][]mat,intk) { |
| 3 | +PriorityQueue<int[]>pq = |
| 4 | +newPriorityQueue<>(Comparator.comparingInt((int[]o) ->o[1]).thenComparingInt(o ->o[0])); |
| 5 | +for (inti =0;i <mat.length;i++) { |
| 6 | +pq.add(newint[]{i,findNumberOfSoldiers(mat[i])}); |
| 7 | + } |
| 8 | +int[]result =newint[k]; |
| 9 | +for (inti =0;i <k && !pq.isEmpty();i++){ |
| 10 | +result[i] =pq.poll()[0]; |
| 11 | + } |
| 12 | +returnresult; |
7 | 13 | }
|
8 |
| -int[]result =newint[k]; |
9 |
| -for (inti =0;i <result.length && !pq.isEmpty();i++) { |
10 |
| -result[i] =pq.poll()[0]; |
11 |
| - } |
12 |
| -returnresult; |
13 |
| - } |
14 | 14 |
|
15 |
| -privateintgetNumberOfOnes(int[]arr) { |
16 |
| -intstart =0; |
17 |
| -intend =arr.length -1; |
18 |
| -while (start <=end) { |
19 |
| -intmid = (start +end) /2; |
20 |
| -if (arr[mid] ==0) { |
21 |
| -end =mid -1; |
22 |
| - }else { |
23 |
| -start =mid +1; |
24 |
| - } |
| 15 | +privateintfindNumberOfSoldiers(int[]row) { |
| 16 | +intleft =0; |
| 17 | +intright =row.length -1; |
| 18 | +while (left <=right) { |
| 19 | +intmid = (left +right) /2; |
| 20 | +if (row[mid] ==0) { |
| 21 | +right =mid -1; |
| 22 | + }else { |
| 23 | +left =mid +1; |
| 24 | + } |
| 25 | + } |
| 26 | +returnright <0 ?0 :left; |
25 | 27 | }
|
26 |
| -returnend <0 ?0 :start; |
27 |
| - } |
28 | 28 | }
|