Input Format

The first line contains an integer, , describing the total number of events. Each of the  subsequent lines will be of the following two forms:

  • ENTER name CGPA id: The student to be inserted into the priority queue.
  • SERVED: The highest priority student in the queue was served.

The locked stub code in the editor reads the input and tests the correctness of the Student and Priorities classes implementation.

Constraints

Output Format

The locked stub code prints the names of the students yet to be served in the priority order. If there are no such student, then the code prints EMPTY.


hackerrank java priority queue solution


Java Priority queue problem solution | HackerRank

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

class Student {
    private final int id;
    private final String name;
    private final double cgpa;

    public Student(int id, String name, double cgpa) {
        this.id = id;
        this.name = name;
        this.cgpa = cgpa;
    }
    
    public int getID() {
       return id;
    }
    
    public String getName() {
       return name;
    }
    
    public double getCGPA() {
       return cgpa;
    }
}

class StudentComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        if (o1.getCGPA() == o2.getCGPA()) {
            if(o1.getName().equals(o2.getName())) {
                return Integer.compare(o2.getID(), o1.getID());
            } else {
                return o1.getName().compareTo(o2.getName());
            } 
        }
        return Double.compare(o2.getCGPA(), o1.getCGPA());    
    }
}

class Priorities {
    public List<Student> getStudents(List<String> events) {
        PriorityQueue pq = new PriorityQueue<>(events.size(), new StudentComparator());
        
        for (String event: events) {
            String parts[] = event.split(" ");
            
            if (parts[0].equals("ENTER")) {
            Student stu = new Student(Integer.parseInt(parts[3]), parts[1], Double.parseDouble(parts[2]));
                pq.add(stu);
            } else {
                if (!pq.isEmpty()) {
                   pq.poll();
                }     
            }
        }
        
        List<Student> students = new ArrayList<>();
        while (!pq.isEmpty()) {
            students.add((Student) pq.poll());
        }
        
        return students;
    }
}


Second solution

import java.util.Comparator;
import java.util.PriorityQueue;

class Student {
    private final int id;
    private final String name;
    private final double cgpa;
    
    public Student(int id, String name, double cgpa) {
        this.id = id;
        this.name = name;
        this.cgpa = cgpa;
    }
    
    public int getID() {
        return id;
    }
    
    public String getName() {
        return name;
    }
    
    public double getCGPA() {
        return cgpa;
    }
}

class Priorities {
    private final PriorityQueue<Student> queue = new PriorityQueue<>(
            Comparator.comparing(Student::getCGPA).reversed()
                    .thenComparing(Student::getName)
                    .thenComparing(Student::getID));
    
    public List<Student> getStudents(List<String> events) {
        events.forEach((event) -> {
            if (event.equals("SERVED")) {
                queue.poll();
            } else {
                String[] details = event.split(" ");
                
        queue.add(new Student(Integer.parseInt(details[3]), details[1], Double.parseDouble(details[2])));
            }
        });
        
        List<Student> students = new ArrayList<>();
        while (!queue.isEmpty()) {
            students.add(queue.poll());
        }
        
        return students;
    }
}