-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathK_weakest_rows.java
58 lines (47 loc) · 1.36 KB
/
K_weakest_rows.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
package Heaps;
import java.util.PriorityQueue;
public class K_weakest_rows {
public static class Row implements Comparable<Row> {
int index;
int sold;
public Row(int index, int sold) {
this.index = index;
this.sold = sold;
}
@Override
public int compareTo(Row r2){
if (this.sold == r2.sold){
return this.index - r2.index;
}else{
return this.sold - r2.sold;
}
}
}
public static void kWeakestRows(int[][] arr, int k){
PriorityQueue<Row> pq = new PriorityQueue<>();
for (int i=0; i<arr.length; i++){
int count = 0;
for (int j=0; j<arr.length; j++){
if (arr[i][j] == 1){
count++;
}
}
pq.add(new Row(i, count));
}
while (k > 0){
System.out.println("Row" + pq.peek().index + " with strength " + pq.peek().sold);
pq.remove();
k--;
}
}
public static void main(String[] args) {
int[][] army = {
{1, 0, 0, 0},
{1, 1, 1, 1},
{1, 0, 0, 0},
{1, 0, 0, 0}
};
int k = 2;
kWeakestRows(army, k);
}
}