You are viewing a single comment's thread. Return to all comments →
public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int totalEvents = Integer.parseInt(scanner.nextLine()); List<String> events = new ArrayList<>(totalEvents); while (totalEvents-- > 0) { events.add(scanner.nextLine()); } scanner.close(); List<Student> students = new Priorities().getStudents(events); if (!students.isEmpty()) { students.forEach(st -> System.out.println(st.getName())); } else { System.out.println("EMPTY"); } } } class Student { 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; } } class Priorities { public List<Student> getStudents(List<String> events) { PriorityQueue<Student> students = new PriorityQueue<>( Comparator.comparing(Student::getCGPA, Comparator.reverseOrder()) .thenComparing(Student::getName) .thenComparing(Student::getId)); for (String event : events) { if (event.equals("SERVED")) { students.poll(); } else { String[] data = event.split(" "); students.offer( new Student(Integer.parseInt(data[3]), data[1], Double.parseDouble(data[2]))); } } List<Student> result = new ArrayList<>(); while (!students.isEmpty()) { result.add(students.poll()); } return result; } }
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 →