We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
class Student implements Comparable {
private int id;
private String name;
private double cgpa;
public Student(int id, String name, double cgpa) {
this.id = id;
this.name = name;
this.cgpa = cgpa;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getCgpa() {
return cgpa;
}
@Override
public int compareTo(Student other) {
if (this.cgpa != other.cgpa) {
return Double.compare(other.cgpa, this.cgpa);
} else if (!this.name.equals(other.name)) {
return this.name.compareTo(other.name);
} else {
return Integer.compare(this.id, other.id);
}
}
}
class Priorities {
public List getStudents(List events) {
PriorityQueue queue = new PriorityQueue();
for (String event : events) {
String[] parts = event.split(" ");
String type = parts[0];
if (type.equals("ENTER")) {
String name = parts[1];
double cgpa = Double.parseDouble(parts[2]);
int id = Integer.parseInt(parts[3]);
queue.add(new Student(id, name, cgpa));
} else if (type.equals("SERVED")) {
queue.poll();
}
}
List<Student> result = new ArrayList<Student>();
while (!queue.isEmpty()) {
result.add(queue.poll());
}
return result;
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Priority Queue
You are viewing a single comment's thread. Return to all comments →
import java.util.PriorityQueue;
class Student implements Comparable { private int id; private String name; private double cgpa;
}
class Priorities { public List getStudents(List events) { PriorityQueue queue = new PriorityQueue();
}