Java Priority Queue
Java Priority Queue
+ 0 comments import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.PriorityQueue;
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; } @Override public int compareTo(Student other) { if (this.cgpa != other.cgpa) { return Double.compare(other.cgpa, this.cgpa); } else if (!(this.name.equals(other.name))) { return this.name.compareTo(other.name); } else { return this.id - other.id; } } 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 > queue = new PriorityQueue < > ();
for (String each: events) { String[] arr = each.split(" "); String type = arr[0]; if (type.equalsIgnoreCase("ENTER")) { String name = arr[1]; double cgpa = Double.parseDouble(arr[2]); int id = Integer.parseInt(arr[3]); queue.add(new Student(id, name, cgpa)); } else if (type.equalsIgnoreCase("SERVED")) { queue.poll(); } } List < Student > students = new ArrayList < > (); while (!queue.isEmpty()) { students.add(queue.poll()); } return students; }
}
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
+ 0 comments Here are the solution of Java Priority Queue HackerRank Solution
+ 0 comments //here is code for java-8
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.PriorityQueue; import java.util.Scanner; class Student { private final int id; private final String name; private final 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) { PriorityQueue<Student> queue = new PriorityQueue<>( Comparator.comparing(Student::getCgpa).reversed() .thenComparing(Student::getName) .thenComparing(Student::getId) ); for (String event : events) { String[] parts = event.split("\\s+"); if (parts[0].equals("ENTER")) { String name = parts[1]; double cgpa = Double.parseDouble(parts[2]); int id = Integer.parseInt(parts[3]); Student student = new Student(id, name, cgpa); queue.add(student); } else if (parts[0].equals("SERVED")) { queue.poll(); } } List<Student> students = new ArrayList<>(queue); students.sort(Comparator.comparing(Student::getCgpa).reversed() .thenComparing(Student::getName) .thenComparing(Student::getId) ); return students; } }
+ 0 comments import java.util.PriorityQueue;
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 other) { if (this.cgpa != other.cgpa) { return Double.compare(other.cgpa, this.cgpa); } else if (!this.name.equals(other.name)) { return this.name.compareTo(other.name); } else { return Integer.compare(this.id, other.id); } }
}
class Priorities { public List getStudents(List events) { PriorityQueue queue = new PriorityQueue();
for (String event : events) { String[] parts = event.split(" "); String type = parts[0]; if (type.equals("ENTER")) { String name = parts[1]; double cgpa = Double.parseDouble(parts[2]); int id = Integer.parseInt(parts[3]); queue.add(new Student(id, name, cgpa)); } else if (type.equals("SERVED")) { queue.poll(); } } List<Student> result = new ArrayList<Student>(); while (!queue.isEmpty()) { result.add(queue.poll()); } return result; }
}
Sort 220 Discussions, By:
Please Login in order to post a comment