-
Notifications
You must be signed in to change notification settings - Fork 138
/
Solution.java
52 lines (46 loc) · 1.47 KB
/
Solution.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
import java.util.PriorityQueue;
class Student implements Comparable<Student> {
int id;
String name;
double cgpa;
Student(int id, String name, double cgpa) {
this.id = id;
this.name = name;
this.cgpa = cgpa;
}
public int compareTo(Student b) {
if (this.cgpa > b.cgpa) { return -1; }
if (this.cgpa < b.cgpa) { return 1; }
if (this.cgpa == b.cgpa) {
if (this.name.compareTo(b.name) < 0 ) { return -1; }
if (this.name.compareTo(b.name) > 0) { return 1; }
if (this.name.compareTo(b.name) == 0) {
if (this.id < b.id) { return -1; }
if (this.id > b.id) { return 1; }
}
}
return 0;
}
public String getName() {
return name;
}
}
class Priorities {
Priorities() {}
public List<Student> getStudents(List<String> events) {
PriorityQueue<Student> queue = new PriorityQueue<Student>();
for (int i = 0; i < events.size(); i++) {
if (events.get(i).contains("SERVED")) {
if (queue.size() > 0) { queue.poll(); }
} else {
String[] data = events.get(i).split(" ");
queue.add(new Student(Integer.parseInt(data[3]), data[1], Double.valueOf(data[2])));
}
}
List<Student> result = new ArrayList<>();
while (queue.size() > 0) {
result.add(queue.poll());
}
return result;
}
}