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.*;classStudent{privateintid;privateStringname;privatedoublecgpa;Student(intid,Stringname,doublecgpa){this.id=id;this.name=name;this.cgpa=cgpa;}intgetID(){returnthis.id;}StringgetName(){returnthis.name;}doublegetCgpa(){returnthis.cgpa;}}classStudentComparatorimplementsComparator<Student>{publicintcompare(Students1,Students2){if(s1.getCgpa()<s2.getCgpa())return1;elseif(s1.getCgpa()>s2.getCgpa())return-1;else{returns1.getName().compareTo(s2.getName());}}}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();PriorityQueue<Student>pq=newPriorityQueue<Student>(n+1,newStudentComparator());for(inti=0;i<n;i++){Stringstr=sc.nextLine();Stringoperation=str.split(" ")[0];if(operation.equals("ENTER")){Stringname=str.split(" ")[1];doublecgpa=Double.parseDouble(str.split(" ")[2]);intid=Integer.parseInt(str.split(" ")[3]);pq.add(newStudent(id,name,cgpa));}elseif(operation.equals("SERVED")){pq.poll();}}List<Student>result=newArrayList<>();while(!pq.isEmpty()){result.add(pq.poll());}if(result.isEmpty()){System.out.println("EMPTY");}else{for(Studentst:result){System.out.println(st.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 →
Java 15 solution