Untitled
unknown
plain_text
3 years ago
1.7 kB
4
Indexable
import java.util.PriorityQueue; // * Create the Student and Priorities classes here. // */ class Student { public int id; public String name; public 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; } } class Priorities { PriorityQueue<Student> queue = new PriorityQueue<>((s1, s2) -> { if (s1.getCGPA() > s2.getCGPA()) return -1; else if (s1.getCGPA() < s2.getCGPA()) return 1; else { if (s1.getName().equals(s2.getName())) { if (s1.getID() < s2.getID()) return -1; else return 1; } else return s1.getName().compareTo(s2.getName()); } }); public List<Student> getStudents(List<String> events) { List<Student> list = new ArrayList<>(); for (String event : events) { if (event.startsWith("ENTER")) { String[] arr = event.split(" "); Student student = new Student(Integer.parseInt(arr[3]), arr[1], Double.parseDouble(arr[2])); queue.add(student); } else if (event.startsWith("SERVED")) { queue.poll(); } } while (!queue.isEmpty()) { list.add(queue.poll()); } return list; } }
Editor is loading...