Java Priority Queue

Sort by

recency

|

263 Discussions

|

  • + 0 comments

    My Java 15 solution:

    import java.io.*;
    import java.util.*;
    
    final class Student{
        int id;
        String name;
        double cgpa;
        public Student(int id, String name, double cgpa){
            this.id = id;
            this.name = name;
            this.cgpa = cgpa;
        }
        int getID(){
            return id;
        }
        String getName(){
            return name;
        }
        double getCGPA(){
            return cgpa;
        }
    }
    
    final class PriorityComparator implements Comparator<Student>{
        public int compare(Student o1, Student o2){
            int cgaComparison = Double.compare(o2.getCGPA(), o1.getCGPA());
            if(cgaComparison != 0)return cgaComparison;
            int nameComparison = o1.getName().compareTo(o2.getName());
            if(nameComparison != 0)return nameComparison;
            return Integer.compare(o1.getID(), o2.getID());
        }
    }
    
    final class Priorities{
        List<Student> getStudents(List<String> events){
            Queue<Student> priorityQueue = new PriorityQueue<>(new PriorityComparator());
            for(String e : events){
                String[] splitE = e.split(" ");
                if(splitE[0].equals("ENTER")){
                    Student student = new Student(Integer.valueOf(splitE[3]), splitE[1], Double.valueOf(splitE[2]));
                    priorityQueue.add(student);
                }
                else{
                    priorityQueue.poll();
                } 
            }
            List<Student> students = new ArrayList<Student>();
            while(!priorityQueue.isEmpty()){
                students.add(priorityQueue.poll());
            }
            return students;
        }
    }
    
    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            sc.nextLine();
            List<String> events = new ArrayList<String>();
            for(int i = 0; i < n; i++){
                events.add(sc.nextLine());
            }
            sc.close();
            List<Student> students = new Priorities().getStudents(events);
            if(students.isEmpty())System.out.println("EMPTY");
            else{
                for(Student e : students){
                    System.out.println(e.getName());
                }
            }
        }
    }
    
  • + 0 comments

    java 8 with streams solution:

    class Student{
        private int id;
        private String name;
        private 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;}
        public String toString(){
            return "id: "+ id + " name: "+ name +" cgpa: "+ cgpa;
        }
    }
    
    class Priorities{
        private PriorityQueue<Student> studentQueue;
        private Comparator<Student> comparator = Comparator
                    .comparingDouble(Student::getCGPA).reversed()
                    .thenComparing(Student::getName)
                    .thenComparingInt(Student::getID);
        public Priorities() {
            studentQueue = new PriorityQueue<>(comparator);
        }
        public List<Student> getStudents(List<String> events){
            events.forEach(line ->{
                String[] arr = line.split(" ");
                String action = arr[0];
                switch(action){
                    case "ENTER":
                        String name = arr[1];
                        double cgpa = Double.parseDouble(arr[2]);
                        int id = Integer.parseInt(arr[3]);
                        studentQueue.add(new Student(id, name, cgpa));
                        break;
                    case "SERVED":
                        studentQueue.poll();
                        break;   
                }
                
            });
            return studentQueue.stream()
                .sorted(comparator)
                .collect(Collectors.toList());
        }
    }
    
  • + 0 comments
    public class Solution {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            int totalEvents = Integer.parseInt(scanner.nextLine());
    
            List<String> events = new ArrayList<>(totalEvents);
    
            while (totalEvents-- > 0) {
                events.add(scanner.nextLine());
            }
    
            scanner.close();
    
            List<Student> students = new Priorities().getStudents(events);
    
            if (!students.isEmpty()) {
                students.forEach(st -> System.out.println(st.getName()));
            } else {
                System.out.println("EMPTY");
            }
        }
    }
    
    class Student {
        private int id;
        private String name;
        private 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 {
        public List<Student> getStudents(List<String> events) {
            PriorityQueue<Student> students = new PriorityQueue<>(
                    Comparator.comparing(Student::getCGPA, Comparator.reverseOrder())
                            .thenComparing(Student::getName)
                            .thenComparing(Student::getId));
    
            for (String event : events) {
                if (event.equals("SERVED")) {
                    students.poll();
                } else {
                    String[] data = event.split(" ");
                    students.offer(
                            new Student(Integer.parseInt(data[3]), data[1], Double.parseDouble(data[2])));
                }
            }
    
            List<Student> result = new ArrayList<>();
            
            while (!students.isEmpty()) {
                result.add(students.poll());
            }
    
            return result;
        }
    }
    
  • + 0 comments
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    import java.util.*;
    /*
     * Create the Student and Priorities classes here.
     */
    class Priorities{ 
        private PriorityQueue<Student> queue = new PriorityQueue<>();
        
        public List<Student> getStudents(List<String> events)
        {
            for(String event: events)
            {
                String[] s = event.split(" ");
                if(s[0].equals("ENTER"))
                {
                    String name = s[1];
                    double cgpa = Double.parseDouble(s[2]);
                    int id = Integer.parseInt(s[3]);
                    Student newStudent = new Student(id, name, cgpa);
                    queue.add(newStudent);
                }else if(s[0].equals("SERVED")){
                    queue.poll();
                }
            }
            
            List<Student> remainingStudents = new ArrayList<>();
            while(!queue.isEmpty()){
                remainingStudents.add(queue.poll());
            }
            return remainingStudents;
        }
     }
     
     
    
    class Student implements Comparable<Student>{
        private int id;
        private String name;
        private double cgpa;
        
        public Student(int id, String name, double cgpa)
        {
            this.id = id;
            this.name = name;
            this.cgpa = cgpa;
        }
        
        public int getID(){
            return this.id;
        }
        
        public String getName(){
            return this.name;
        }
        
        public double getCGPA()
        {
            return this.cgpa;
        }
        
        public int compareTo(Student student)
        {
            if(this.cgpa != student.cgpa)
            {
                return Double.compare(student.cgpa, this.cgpa);   
            }else if(!this.name.equals(student.name)){
                return this.name.compareTo(student.name);
            }else{
                return Integer.compare(this.id, student.id);
            }
        }
     }
    
    
    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());
                }
            }
        }
    }
    
  • + 0 comments

    Java 15 solution

    import java.io.*;
    import java.util.*;
    
    class Student{
        
         private int id;
         private String name;
         private double cgpa;
         
         Student(int id, String name, double cgpa){
            this.id = id;
            this.name = name;
            this.cgpa = cgpa;
         }
         
         int getID(){
            return this.id;
         }
         
         String getName(){
            return this.name;
         }
         
         double getCgpa(){
            return this.cgpa;
         }
    }
    
    class StudentComparator implements Comparator<Student>{
        
        public int compare(Student s1, Student s2){
            if(s1.getCgpa() < s2.getCgpa()) return 1;
            else if( s1.getCgpa() > s2.getCgpa()) return -1;
            else{
                return s1.getName().compareTo(s2.getName());
            }
        }
    }
    
    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            sc.nextLine();
            PriorityQueue<Student> pq = new PriorityQueue<Student>(n+1, new StudentComparator());
                    
            for(int i=0;i<n;i++){
                String str = sc.nextLine();
                String operation = str.split(" ")[0];
                if(operation.equals("ENTER")){
                    String name = str.split(" ")[1];
                    double cgpa = Double.parseDouble(str.split(" ")[2]);
                    int id = Integer.parseInt(str.split(" ")[3]);
                    pq.add(new Student(id,name,cgpa));   
                }
                else if(operation.equals("SERVED")){
                    pq.poll();
                }
            }
            
                List<Student> result = new ArrayList<>();
                while (!pq.isEmpty()) {
                    result.add(pq.poll());
                }
                    
                if (result.isEmpty()) {
                    System.out.println("EMPTY");
                } else {
                        for (Student st: result) {
                                System.out.println(st.getName());
                }
                
            }
    
        }
    }