We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Java Priority Queue
Java Priority Queue
+ 0 comments Here are the solution of Java Priority Queue Hacker Rank Solution
+ 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 compareTo(Student s){ if(cgpa==s.getCgpa() && name.compareTo(s.getName())==0){ return id-s.getId(); } if(cgpa==s.getCgpa()) { return name.compareTo(s.getName()); } if(cgpa>s.getCgpa()){ return -1; } else{ return 1; } } 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> pq = new PriorityQueue<>(); for (String str : events) { String[] event =str.split(" "); switch(event[0]){ case "ENTER":{ String name= event[1]; int id= Integer.valueOf(event[3]); double cgpa= Double.valueOf(event[2]); Student student = new Student(id,name,cgpa ); pq.add(student); break; } case "SERVED":{ pq.poll(); break; } } } ArrayList<Student> temp = new ArrayList<>(); while (!pq.isEmpty()) { temp.add(pq.poll()); } return temp; } }
+ 0 comments Here is problem solution - https://thecscience.com/hackerrank-java-priority-queue-problem-solution.html
+ 0 comments To be honest, i have no idea why thats not working 😅
class Priorities { public List<Student> getStudents(List<String> events) { PriorityQueue<Student> response = new PriorityQueue<>( Comparator.comparing(Student::getCGPA, Comparator.reverseOrder()) .thenComparing(Student::getName) .thenComparing(Student::getID)); for (String event : events) { if (!event.contains("ENTER")) { response.poll(); continue; } String[] split = event.split(" "); Integer id = Integer.valueOf(split[3]); Double cgpa = Double.valueOf(split[2]); Student student = new Student(id, split[1], cgpa); response.add(student); } return response.stream().collect(Collectors.toList()); } }
+ 0 comments Why my code doesn't work?
import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.Queue; import java.util.PriorityQueue; import java.util.stream.Collectors; /* * Create the Student and Priorities classes here. */ 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 id; } public String getName(){ return name; } public double getCGPA(){ return cgpa; } @Override public int compareTo(Student o){ int cgpaComparison = - Double.compare(cgpa, o.cgpa); int nameComparison = name.compareTo(o.name); if(cgpa != 0) return cgpaComparison; if(nameComparison != 0 ) return nameComparison; return Integer.compare(id, o.id); } } class Priorities { private Queue<Student> priorityQueue; public Priorities (){ priorityQueue = new PriorityQueue<>(); } List<Student> getStudents(List<String> events){ for(String event: events){ String[] parts = event.split(" "); if(parts.length == 1){ // served event priorityQueue.poll(); } else if(parts.length == 4){ // enter event String name = parts[1]; double cgpa = Double.parseDouble(parts[2]); int id = Integer.parseInt(parts[3]); Student student = new Student(id, name, cgpa); priorityQueue.add(student); } else { throw new IllegalArgumentException(); } } return priorityQueue.stream().sorted().collect(Collectors.toList()); } } 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()); } } } }
Load more conversations
Sort 211 Discussions, By:
Please Login in order to post a comment