-
Notifications
You must be signed in to change notification settings - Fork 858
Expand file tree
/
Copy pathJavaPriorityQueue.java
More file actions
96 lines (85 loc) · 1.91 KB
/
Copy pathJavaPriorityQueue.java
File metadata and controls
96 lines (85 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
*
*/
package com.javaaid.hackerrank.solutions.languages.java.datastructures;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class JavaPriorityQueue {
static class Student {
private int token;
private String fname;
private double cgpa;
public Student(int id, String fname, double cgpa) {
super();
this.token = id;
this.fname = fname;
this.cgpa = cgpa;
}
public int getToken() {
return token;
}
public String getFname() {
return fname;
}
public double getCgpa() {
return cgpa;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
PriorityQueue<Student> data = new PriorityQueue<Student>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if (o1.getCgpa() < o2.getCgpa()) {
return 1;
} else if (o1.getCgpa() > o2.getCgpa()) {
return -1;
} else {
if (o1.getFname().compareTo(o2.getFname()) == 0) {
if (o1.getToken() > o2.getToken()) {
return 1;
} else if (o1.getToken() < o2.getToken()) {
return -1;
} else {
return 0;
}
} else {
return o1.getFname().compareTo(o2.getFname());
}
}
}
});
for (int i = 0; i < t; i++) {
String op = sc.next();
switch (op) {
case "ENTER":
String name = sc.next();
double cgpa = sc.nextDouble();
int id = sc.nextInt();
Student s = new Student(id, name, cgpa);
data.add(s);
break;
case "SERVED":
if (data.isEmpty()) {
break;
}
data.remove();
}
}
if (data.isEmpty())
System.out.println("EMPTY");
else {
while (!data.isEmpty()) {
Student st = data.poll();
System.out.println(st.getFname());
}
}
sc.close();
}
}