Java Priority Queue

Sort by

recency

|

273 Discussions

|

  • + 0 comments

    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 getStudents(List events) {

        PriorityQueue<Student> queue = new PriorityQueue<>(new Comparator<Student>() {
            public int compare(Student s1, Student s2) {
                if (s1.getCGPA() != s2.getCGPA())
                    return Double.compare(s2.getCGPA(), s1.getCGPA());
                else if (!s1.getName().equals(s2.getName()))
                    return s1.getName().compareTo(s2.getName());
                else
                    return s1.getID() - s2.getID();
            }
        });
    
        for (String event : events) {
            String[] parts = event.split(" ");
    
            if (parts[0].equals("ENTER")) {
                queue.add(new Student(
                    Integer.parseInt(parts[3]),
                    parts[1],
                    Double.parseDouble(parts[2])
                ));
            } else if (parts[0].equals("SERVED")) {
                if (!queue.isEmpty())
                    queue.poll();
            }
        }
    
        List<Student> students = new ArrayList<>();
        while (!queue.isEmpty()) {
            students.add(queue.poll());
        }
    
        return students;
    }
    
  • + 0 comments

    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 getStudents(List events) {

        PriorityQueue<Student> queue = new PriorityQueue<>(new Comparator<Student>() {
            public int compare(Student s1, Student s2) {
                if (s1.getCGPA() != s2.getCGPA())
                    return Double.compare(s2.getCGPA(), s1.getCGPA());
                else if (!s1.getName().equals(s2.getName()))
                    return s1.getName().compareTo(s2.getName());
                else
                    return s1.getID() - s2.getID();
            }
        });
    
        for (String event : events) {
            String[] parts = event.split(" ");
    
            if (parts[0].equals("ENTER")) {
                queue.add(new Student(
                    Integer.parseInt(parts[3]),
                    parts[1],
                    Double.parseDouble(parts[2])
                ));
            } else if (parts[0].equals("SERVED")) {
                if (!queue.isEmpty())
                    queue.poll();
            }
        }
    
        List<Student> students = new ArrayList<>();
        while (!queue.isEmpty()) {
            students.add(queue.poll());
        }
    
    correct output for this program 
    
        return students;
    }
    

    }

  • + 0 comments
    import java.util.PriorityQueue;
    import java.util.Comparator;
    
    class Student {
        private int id;
        private String name;
        private double cgpa;
        
        public int getId(){return this.id;}
        public String getName(){return this.name;}
        public double getCGPA(){ return this.cgpa;}
        
        public Student(String id, String name, String cgpa){
            this.id = Integer.parseInt(id);
            this.name = name;
            this.cgpa = Double.parseDouble(cgpa);
        }
     }
     
    class Priorities {
        // No attributes required
        public List<Student> getStudents(List<String> events){
            List<Student> out = new ArrayList<>();
            PriorityQueue<Student> pq = new PriorityQueue<Student>(
                Comparator.comparing(Student::getCGPA, Comparator.reverseOrder())
                .thenComparing(Student::getName)
                .thenComparing(Student::getId));
    
            // O(n log k)
            events.forEach(eventString -> 
            {
                String[] parts = eventString.split(" ");
                if("SERVED".equals(parts[0])){
                    pq.poll();
                }else{
                    Student student = new Student(parts[3], parts[1], parts[2]);
                    pq.add(student);
                }
            });
            
            // O(m log m)
            while(!pq.isEmpty()){
                out.add(pq.poll());
            }
            
            // Overall complexity: O(n log k) when k < n,
            // worst scenario O(n log n) when n = k
            return out;
        }
     }
    
  • + 0 comments

    Nice problem — it really checks whether you understand how ordering works inside a priority queue rather than just using it blindly. Telugu 365 ID Login

  • + 0 comments

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

    import java.util.Comparator; import java.util.PriorityQueue; import java.util.Scanner; import java.util.*; class Student implements Comparable{ 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 getStudents(List events) { int n = events.size(); PriorityQueue pq = new PriorityQueue(); 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(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());
            }
        }
    }
    

    }