Java Priority Queue
Problem Statement :
In computer science, a priority queue is an abstract data type which is like a regular queue, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. - Wikipedia In this problem we will test your knowledge on Java Priority Queue. There are a number of students in a school who wait to be served. Two types of events, ENTER and SERVED, can take place which are described below. ENTER: A student with some priority enters the queue to be served. SERVED: The student with the highest priority is served (removed) from the queue. A unique id is assigned to each student entering the queue. The queue serves the students based on the following criteria (priority criteria): 1.The student having the highest Cumulative Grade Point Average (CGPA) is served first. 2.Any students having the same CGPA will be served by name in ascending case-sensitive alphabetical order. 3.Any students having the same CGPA and name will be served in ascending order of the id. Create the following two classes: The Student class should implement: The constructor Student(int id, String name, double cgpa). The method int getID() to return the id of the student. The method String getName() to return the name of the student. The method double getCGPA() to return the CGPA of the student. The Priorities class should implement the method List<Student> getStudents(List<String> events) to process all the given events and return all the students yet to be served in the priority order. Input Format The first line contains an integer, n, describing the total number of events. Each of the n subsequent lines will be of the following two forms: ENTER name CGPA id: The student to be inserted into the priority queue. SERVED: The highest priority student in the queue was served. The locked stub code in the editor reads the input and tests the correctness of the Student and Priorities classes implementation. Constraints 2<=n<=1000 0<=CGPA<=4.00 1<=id<=10^5 2<=|name|<=30 Output Format The locked stub code prints the names of the students yet to be served in the priority order. If there are no such student, then the code prints EMPTY.
Solution :
Solution in C :
import java.util.Comparator;
import java.util.PriorityQueue;
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 {
private final PriorityQueue<Student> queue = new PriorityQueue<>(
Comparator.comparing(Student::getCGPA).reversed()
.thenComparing(Student::getName)
.thenComparing(Student::getID));
public List<Student> getStudents(List<String> events) {
events.forEach((event) -> {
if (event.equals("SERVED")) {
queue.poll();
} else {
String[] details = event.split(" ");
queue.add(new Student(Integer.parseInt(details[3]), details[1], Double.parseDouble(details[2])));
}
});
List<Student> students = new ArrayList<>();
while (!queue.isEmpty()) {
students.add(queue.poll());
}
return students;
}
}
View More Similar Problems
Self-Driving Bus
Treeland is a country with n cities and n - 1 roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between ever
View Solution →Unique Colors
You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti
View Solution →Fibonacci Numbers Tree
Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T
View Solution →Pair Sums
Given an array, we define its value to be the value obtained by following these instructions: Write down all pairs of numbers from this array. Compute the product of each pair. Find the sum of all the products. For example, for a given array, for a given array [7,2 ,-1 ,2 ] Note that ( 7 , 2 ) is listed twice, one for each occurrence of 2. Given an array of integers, find the largest v
View Solution →Lazy White Falcon
White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi
View Solution →Ticket to Ride
Simon received the board game Ticket to Ride as a birthday present. After playing it with his friends, he decides to come up with a strategy for the game. There are n cities on the map and n - 1 road plans. Each road plan consists of the following: Two cities which can be directly connected by a road. The length of the proposed road. The entire road plan is designed in such a way that if o
View Solution →