In this HackerRank Java Priority Queue problem in the java programming language you need to Create the following two classes:
The Student class should implement:
- The constructor Student(int id, String name, double cgpa).
- The method int getID() to return the id of the student.
- The method String getName() to return the name of the student.
- The method double getCGPA() to return the CGPA of the student.
The Priorities class should implement the method List<Student> getStudents(List<String> events) to process all the given events and return all the students yet to be served in the priority order.
HackerRank Java Priority Queue problem solution.
import java.util.ArrayList; import java.util.List; import java.util.Scanner; /* * Create the Student and Priorities classes here. */ import java.util.Comparator; import java.util.PriorityQueue; import java.util.Scanner; import java.util.*; class Student implements Comparable<Student>{ String name = new String(); double cgpa; int id; public Student(String name,double cgpa,int id) { this.name = name; this.cgpa = cgpa; this.id = id; } public String getName(){ return this.name; } public int compareTo(Student s) { if(cgpa == s.cgpa) { if(name.compareTo(s.name) == 0) { if(id == s.id) return 0; else if (id > s.id) return 1; else return -1; } else return name.compareTo(s.name); } else if(cgpa > s.cgpa) return -1; else return 1; } } class Priorities{ public ArrayList<Student> getStudents(List<String> events) { int n = events.size(); PriorityQueue<Student> pq = new PriorityQueue<Student>(); for(String i:events) { String[] s = new String[4]; s = i.split("\s"); if(s.length>1) { pq.add(new Student(s[1],Double.valueOf(s[2]),Integer.valueOf(s[3]))); } else { pq.poll(); } } while(pq.size()>1) { System.out.println(pq.poll().name); } return new ArrayList<Student>(pq); } } public class Solution { private final static Scanner scan = new Scanner(System.in); private final static Priorities priorities = new Priorities(); public static void main(String[] args) { int totalEvents = Integer.parseInt(scan.nextLine()); List<String> events = new ArrayList<>(); while (totalEvents-- != 0) { String event = scan.nextLine(); events.add(event); } List<Student> students = priorities.getStudents(events); if (students.isEmpty()) { System.out.println("EMPTY"); } else { for (Student st: students) { System.out.println(st.getName()); } } } }
Second solution
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; class Student{ private int token; private String fname; private double cgpa; public Student(int id, String fname, double cgpa) { super(); this.token = id; this.fname = fname; this.cgpa = cgpa; } public int getToken() { return token; } public String getFname() { return fname; } public double getCgpa() { return cgpa; } } class StudentComparator implements Comparator<Student> { public int compare(Student s1, Student s2) { if (s1.getCgpa() > s2.getCgpa()) { return -1; } if (s1.getCgpa() < s2.getCgpa()) { return 1; } if (s1.getFname().compareTo(s2.getFname()) < 0) { return -1; } if (s1.getFname().compareTo(s2.getFname()) > 0) { return 1; } if (s1.getToken() < s2.getToken()) { return -1; } if (s1.getToken() > s2.getToken()) { return 1; } return 0; } } public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int totalEvents = Integer.parseInt(in.nextLine()); PriorityQueue<Student> pq = new PriorityQueue(totalEvents, new StudentComparator()); while(totalEvents>0){ String event = in.next(); if (event.equals("ENTER")) { String fname = in.next(); double cgpa = in.nextDouble(); int id = in.nextInt(); Student s = new Student(id, fname, cgpa); pq.add(s); } else { pq.poll(); } totalEvents--; } if (pq.isEmpty()) { System.out.println("EMPTY"); } while (!pq.isEmpty()) { System.out.println(pq.poll().getFname()); } } }
The solution in java programming
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; class Student{ private int token; private String fname; private double cgpa; public Student(int id, String fname, double cgpa) { super(); this.token = id; this.fname = fname; this.cgpa = cgpa; } public int getToken() { return token; } public String getFname() { return fname; } public double getCgpa() { return cgpa; } } public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int totalEvents = Integer.parseInt(in.nextLine()); PriorityQueue<Student> list = new PriorityQueue<>(new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { if(o1.getCgpa() != o2.getCgpa()) return o1.getCgpa() < o2.getCgpa() ? 1 : -1; else{ if(!o1.getFname().equals(o2.getFname())) return o1.getFname().compareTo(o2.getFname()); else return o1.getToken() < o2.getToken() ? 1 : -1; } } }); while(totalEvents>0){ String event = in.next(); if(event.equals("ENTER")){ String name = in.next(); double gpa = in.nextDouble(); int id = in.nextInt(); list.add(new Student(id, name, gpa)); }else if(list.size() > 0 && event.equals("SERVED")){ list.remove(); } totalEvents--; } if(list.size() == 0) { System.out.println("EMPTY"); return; } while(list.size() > 0){ System.out.println(list.poll().getFname()); } } }