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.Queue;importjava.util.PriorityQueue;importjava.util.stream.Collectors;/* * Create the Student and Priorities classes here. */classStudentimplementsComparable<Student>{privateintid;privateStringname;privatedoublecgpa;publicStudent(intid,Stringname,doublecgpa){this.id=id;this.name=name;this.cgpa=cgpa;}publicintgetId(){returnid;}publicStringgetName(){returnname;}publicdoublegetCGPA(){returncgpa;}@OverridepublicintcompareTo(Studento){intcgpaComparison=-Double.compare(cgpa,o.cgpa);intnameComparison=name.compareTo(o.name);if(cgpa!=0)returncgpaComparison;if(nameComparison!=0)returnnameComparison;returnInteger.compare(id,o.id);}}classPriorities{privateQueue<Student>priorityQueue;publicPriorities(){priorityQueue=newPriorityQueue<>();}List<Student>getStudents(List<String>events){for(Stringevent:events){String[]parts=event.split(" ");if(parts.length==1){// served eventpriorityQueue.poll();}elseif(parts.length==4){// enter eventStringname=parts[1];doublecgpa=Double.parseDouble(parts[2]);intid=Integer.parseInt(parts[3]);Studentstudent=newStudent(id,name,cgpa);priorityQueue.add(student);}else{thrownewIllegalArgumentException();}}returnpriorityQueue.stream().sorted().collect(Collectors.toList());}}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 →
Why my code doesn't work?