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.util.ArrayList;importjava.util.List;importjava.util.Scanner;importjava.util.Collections;importjava.util.LinkedList;importjava.util.Comparator;classStudent{intid;Stringname;doublecgpa;publicStudent(intid,Stringname,doublecgpa){this.id=id;this.name=name;this.cgpa=cgpa;}publicintgetID(){returnid;}publicStringgetName(){returnname;}publicdoublegetCGPA(){returncgpa;}}classPriorities{publicList<Student>getStudents(List<String>events){LinkedList<Student>pq=newLinkedList<Student>();for(inti=0;i<events.size();i++){String[]entry=events.get(i).split("\\s+");if(entry[0].equals("ENTER")){Students=newStudent(Integer.parseInt(entry[3]),entry[1],Double.parseDouble(entry[2]));pq.add(s);}elseif(entry[0].equals("SERVED")){//sort the queue firstCollections.sort(pq,newCompareStudents());try{pq.removeFirst();}catch(Exceptione){};}}returnpq;}}classCompareStudentsimplementsComparator<Student>{publicintcompare(Students1,Students2){//sorting logicif(s1.cgpa<s2.cgpa){return1;}if(s1.cgpa>s2.cgpa){return-1;}if(s1.name.compareTo(s2.name)!=0){returns1.name.compareTo(s2.name);}if(s1.id<s2.id){return-1;}if(s1.id>s2.id){return1;}return0;}}publicclassSolution{privatefinalstaticScannerscan=newScanner(System.in);privatefinalstaticPrioritiespriorities=newPriorities();publicstaticvoidmain(String[]args){inttotalEvents=Integer.parseInt(scan.nextLine());List<String>events=newArrayList<>();while(totalEvents--!=0){Stringevent=scan.nextLine();events.add(event);}List<Student>students=priorities.getStudents(events);if(students.isEmpty()){System.out.println("EMPTY");}else{for(Studentst:students){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 →
using a linked list for the queue