-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_174_Weakest_Soldier.java
More file actions
52 lines (41 loc) · 1.29 KB
/
a_174_Weakest_Soldier.java
File metadata and controls
52 lines (41 loc) · 1.29 KB
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
import java.util.* ;
public class a_174_Weakest_Soldier {
static class Row implements Comparable<Row> {
int soldier ;
int idx ;
public Row(int soldier, int idx ){
this.soldier = soldier ;
this.idx = idx ;
}
@Override
public int compareTo(Row r2){
if(this.soldier == r2.soldier){
return this.idx - r2.idx ;
}
else{
return this.soldier = r2.soldier ;
}
}
}
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 = 4 ;
PriorityQueue<Row> pq = 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 ;
if(army[i][j] == 1){
count++ ;
}
}
pq.add( new Row(count, i)) ;
}
for(int i=0; i<k; i++){
System.out.println("R" + pq.remove().idx );
}
}
}