Java Priority Queue

Sort by

recency

|

257 Discussions

|

  • + 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.

  • + 0 comments

    In java 15:

    import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.*;

    class Priorities {

    List<Student> getStudents(List<String> events) {
    
        PriorityQueue<Student> priorityQueue = new PriorityQueue<>(new Comparator<Student>() {
            public int compare(Student s1, Student s2) {
                return s1.getCGPA() != s2.getCGPA() ? s2.getCGPA() - s1.getCGPA() > 0 ? 1 : -1 : !s1.getName().equals(s2.getName()) ? s1.getName().compareTo(s2.getName()) :  s1.getID() > s2.getID() ? 1 : -1;
            }
        });
    
        for (String str: events) {
            String isENTER = str.split(" ")[0];
    
            if (isENTER.equals("ENTER")) {
                int id = Integer.parseInt(str.split(" ")[3]);
                String name = str.split(" ")[1];
                double cgpa = Double.parseDouble(str.split(" ")[2]);
    
                priorityQueue.add(new Student(id, name, cgpa));
    
            } else priorityQueue.poll();
        }
    
        List<Student> result = new ArrayList<>();
    
        while (!priorityQueue.isEmpty()) {
            result.add(priorityQueue.poll());
        }
    
        return result;
    }
    

    }

    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 id;
    }
    
    String getName() {
        return name;
    }
    
    double getCGPA() {
        return cgpa;
    }
    

    }

    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

    class Student implements Comparable{

     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;
     }
    
     @Override
     public int compareTo(Student s){
         if(cgpa==s.cgpa){
                if(name.compareTo(s.name)!=0){
                    return name.compareTo(s.name);
                }else if(id>s.id){
                    return 1;
                }else{
                    return -1;
                }
            }else if(cgpa<s.cgpa){
                return 1;
            }else{
                return -1;
            }
     }
    

    }

    class Priorities{

     private PriorityQueue<Student> pq;
     public Priorities(){
         pq = new PriorityQueue<>();
     }
    
     public List<Student> getStudents(List<String> events){
         List<Student> result = new ArrayList<>();
         for(String event : events){
             String[] s = event.split(" ");
             String op = s[0];
    
             if(op.equals("ENTER")){
                 Student stu = new Student(Integer.parseInt(s[3]),s[1],Double.parseDouble(s[2]));
                 pq.add(stu);
             }else if(op.equals("SERVED")){
                 pq.poll();
             }
         }
         Iterator iter = pq.iterator();
         while(iter.hasNext()){
             result.add(pq.poll());
         }
    
             return result;
         }
     }
    
    
    
         return result;
     }
    

    }