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.
importjava.io.*;importjava.util.*;finalclassStudent{intid;Stringname;doublecgpa;publicStudent(intid,Stringname,doublecgpa){this.id=id;this.name=name;this.cgpa=cgpa;}intgetID(){returnid;}StringgetName(){returnname;}doublegetCGPA(){returncgpa;}}finalclassPriorityComparatorimplementsComparator<Student>{publicintcompare(Studento1,Studento2){intcgaComparison=Double.compare(o2.getCGPA(),o1.getCGPA());if(cgaComparison!=0)returncgaComparison;intnameComparison=o1.getName().compareTo(o2.getName());if(nameComparison!=0)returnnameComparison;returnInteger.compare(o1.getID(),o2.getID());}}finalclassPriorities{List<Student>getStudents(List<String>events){Queue<Student>priorityQueue=newPriorityQueue<>(newPriorityComparator());for(Stringe:events){String[]splitE=e.split(" ");if(splitE[0].equals("ENTER")){Studentstudent=newStudent(Integer.valueOf(splitE[3]),splitE[1],Double.valueOf(splitE[2]));priorityQueue.add(student);}else{priorityQueue.poll();}}List<Student>students=newArrayList<Student>();while(!priorityQueue.isEmpty()){students.add(priorityQueue.poll());}returnstudents;}}publicclassSolution{publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scannersc=newScanner(System.in);intn=sc.nextInt();sc.nextLine();List<String>events=newArrayList<String>();for(inti=0;i<n;i++){events.add(sc.nextLine());}sc.close();List<Student>students=newPriorities().getStudents(events);if(students.isEmpty())System.out.println("EMPTY");else{for(Studente:students){System.out.println(e.getName());}}}}
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 →
My Java 15 solution: