-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweakestSoldier.java
41 lines (38 loc) · 1.06 KB
/
weakestSoldier.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
package Heap;
import java.util.*;
public class weakestSoldier {
static class Row implements Comparable<Row>{
int soldiers;
int idx;
public Row(int soldiers, int idx){
this.soldiers=soldiers;
this.idx=idx;
}
@Override
public int compareTo(Row r2){
if(this.soldiers==r2.soldiers){
return this.idx-r2.idx;
}else{
return this.soldiers-r2.soldiers;
}
}
}
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;
PriorityQueue<Row> p=new PriorityQueue<>();
for(int i=0;i<army.length;i++){
int count =0;
for(int j=0;j<army[0].length;j++){
count +=army[i][j]==1 ? 1:0;
}
p.add(new Row(count,i));
}
for(int i=0;i<k;i++){
System.out.println("R"+p.remove().idx);
}
}
}