Java Priority Queue

Sort by

recency

|

259 Discussions

|

  • + 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());
                }
                
            }
    
        }
    }
    
  • + 0 comments

    My solution with java 8

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    /*
     * Create the Student and Priorities classes here.
     */
    import java.util.*;
    class Student {
        public int id;
        public String name;
        public 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 s1, Student s2) {
    
            if(s1.cgpa == s2.cgpa){
                return s1.name.compareTo(s2.name);
            } else if (s1.cgpa == s2.cgpa && s1.name == s2.name){
                if(s1.id > s2.id)
                    return 1;
                else
                    return -1;
            } else if(s1.cgpa < s2.cgpa){
                return 1;
            } else if(s1.cgpa > s2.cgpa){
                return -1;
            } else {
                return 0;
            }
        }
    }
    class Priorities{
        public PriorityQueue<Student> q = new PriorityQueue<>(new StudentComparator());
        List<Student> student2List = new ArrayList<>();
    
        public List<Student> getStudents(List<String> events){
            for(String st : events){
    
                String[] parts = st.split(" ");
    
                if(parts[0].equals("ENTER")) {
                    q.add(new Student(Integer.parseInt(parts[3]), parts[1], Double.parseDouble(parts[2])));
    
                } else if (parts[0].equals("SERVED")){
                    q.poll();
                }
            }
    
            while(!q.isEmpty()){
                student2List.add(q.poll());
            }
    
            return student2List;
        }
    }
    
    
    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

    using a linked list for the queue

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    import java.util.Collections;
    import java.util.LinkedList;
    import java.util.Comparator;
    
    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;
        }
        public int getID(){
            return id;
        }
        public String getName(){
            return name;
        }
        public double getCGPA(){
            return cgpa;
        }
    }
    
    class Priorities{
        public List<Student> getStudents(List<String> events){
            LinkedList<Student> pq = new LinkedList<Student>(); 
      
            for(int i=0;i<events.size();i++){
                String[] entry = events.get(i).split("\\s+");
                
                if(entry[0].equals("ENTER")){
    
                    Student s = new Student(Integer.parseInt(entry[3]), entry[1], Double.parseDouble(entry[2]));
                    pq.add(s);
                    
                    
                }
                
                else if(entry[0].equals("SERVED")){
                    //sort the queue first
                    Collections.sort(pq, new CompareStudents());
                    try{pq.removeFirst();}catch(Exception e){};
                }
            }
            return pq;
        }
    }
    
    class CompareStudents implements Comparator<Student>{
        public int compare(Student s1, Student s2){
            //sorting logic
            if(s1.cgpa<s2.cgpa){
                return 1;
            }
            if(s1.cgpa>s2.cgpa){
                return -1;
            }
            if(s1.name.compareTo(s2.name)!=0){
                return s1.name.compareTo(s2.name);
            }
           
            if(s1.id<s2.id){
                return -1;
            }
            if(s1.id>s2.id){
                return 1;
            }
            return 0;
        }
    }
    
    
    
    
    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
    import java[dot]util[dot]PriorityQueue ;
    import java[dot]util[dot]LinkedList ;
    class Student {
        private int id;
        private String name;
        private double cgpa;
        public Student(String name, double cgpa,int id) {
            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 PriorityQueue<Student> queue;
        public Priorities() {
            queue = new PriorityQueue<>((s1, s2) -> {
                int[] res = {Double.compare(s2.getCGPA(),
                s1.getCGPA()),s1.getName().compareTo(s2.getName()),
                Integer.compare(s1.getID(), s2.getID())};
                for (int r : res){if (r != 0){return r;}}
                return 0;});
        }
        public List<Student> getStudents(List<String> events) {
            for (String event : events) {String[] e = event.split(" ");
                if (e[0].equals("ENTER")) 
                {queue.add(
                    new Student(e[1], Double.parseDouble(e[2]),Integer.parseInt(e[3]))
                    );}
                else if (e[0].equals("SERVED")) {queue.poll();}}
                LinkedList<Student> StudentsLeft = new LinkedList<>();
                while (!queue.isEmpty()) {StudentsLeft.add(queue.poll());}
                return StudentsLeft;}
    }
    
  • + 0 comments

    There might be more SERVED events than ENTER events, even though it's logically inconsistent.