Java Priority Queue

Sort by

recency

|

267 Discussions

|

  • + 1 comment
     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;
         }
       @Override
        public int compareTo(Student other) {
        if (this.cgpa != other.cgpa) return Double.compare(other.cgpa, this.cgpa);
        int nameComp = this.name.compareTo(other.name);
        if (nameComp != 0) return nameComp;
        return this.id - other.id;
    }
     }
     class Priorities {
        public List<Student> getStudents(List<String> events) {
            int idStudent = 0;
            PriorityQueue<Student> queue = new PriorityQueue<>();
            for (int i = 0; i < events.size(); i++) {
                if (events.get(i).startsWith("ENTER")) {
                    String[] parts = events.get(i).split(" ");
                    String name = parts[1];
                    double cgpa = Double.parseDouble(parts[2]);
                    idStudent++;
                    Student student = new Student(idStudent, name, cgpa);
                    queue.add(student);
                } else if (events.get(i).startsWith("SERVED")) {
                    queue.poll(); 
                }
            }
    
          
            List<Student> result = new ArrayList<>();
            while (!queue.isEmpty()) {
                result.add(queue.poll());
            }
            return result;
        }
    }
    
  • + 0 comments

    Java's PriorityQueue is a queue-based data structure that functions as a min-heap by default, where the head of the queue is the least element according to natural ordering or a specified comparator. It is part of the java.util package and implements the Queue interface. गोल्ड 365 लॉगिन

  • + 0 comments

    Here is Java Priority Queue solution - https://programmingoneonone.com/hackerrank-java-priority-queue-problem-solution.html

  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    class Student {
        int id;
        String name;
        double cgpa;
    
        Student(int id, String name, double cgpa) {
            this.id = id;
            this.name = name;
            this.cgpa = cgpa;
        }
    }
    
    
    
    public class Solution {
        
        public static List<Student> solve(List<String> events){
            PriorityQueue<Student> pq= new PriorityQueue<>((a,b)->{
               if(a.cgpa!=b.cgpa) return Double.compare(b.cgpa, a.cgpa);
               if (!a.name.equals(b.name)) return a.name.compareTo(b.name);
               return Integer.compare(a.id, b.id);
                
            });
            
            for (String event : events) {
                String[] parts = event.split(" ");
                if (parts[0].equals("ENTER")) {
                    String name = parts[1];
                    double cgpa = Double.parseDouble(parts[2]);
                    int id = Integer.parseInt(parts[3]);
                    pq.add(new Student(id, name, cgpa));
                } else if (!pq.isEmpty()) {
                    pq.poll();
                }
            }
    
            List<Student> remaining = new ArrayList<>();
            while (!pq.isEmpty()) remaining.add(pq.poll());
            return remaining;
            
            
        };
    
        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 = Integer.parseInt(sc.nextLine());
            List<String> events = new ArrayList<>();
            while (n-- > 0) events.add(sc.nextLine());
            
            List<Student> st=solve(events);
             if (st.isEmpty()) System.out.println("EMPTY");
            else for (Student s : st) System.out.println(s.name);
            
            
            
            
        }
    }
    
  • + 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());
                }
            }
        }
    }