You are viewing a single comment's thread. Return to all comments →
Simple code
import java.io.; import java.util.;
class Student { private int id; private String fname; private double cgpa;
public Student(int id, String fname, double cgpa) { super(); this.id = id; this.fname = fname; this.cgpa = cgpa; } public int getId() { return id; } public String getFname() { return fname; } public double getCgpa() { return cgpa; }
}
public class Solution {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); PriorityQueue<Student> queue = new PriorityQueue<>(Comparator .comparing(Student::getCgpa).reversed() .thenComparing(Student::getFname) .thenComparingInt(Student::getId)); while (n-- > 0) { String operation = sc.next(); switch (operation) { case "ENTER": String fname = sc.next(); double cgpa = sc.nextDouble(); int id = sc.nextInt(); Student s = new Student(id, fname, cgpa); queue.add(s); break; case "SERVED": queue.poll(); // Remove the student with the highest priority break; } } if (queue.isEmpty()) { System.out.println("EMPTY"); } else { while (!queue.isEmpty()) { System.out.println(queue.poll().getFname()); } } }
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 →
Simple code
import java.io.; import java.util.;
class Student { private int id; private String fname; private double cgpa;
}
public class Solution {
}