-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
136 lines (116 loc) · 3.26 KB
/
Solution.java
File metadata and controls
136 lines (116 loc) · 3.26 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// https://www.hackerrank.com/challenges/java-priority-queue/problem
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/*
* 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; }
public String toString() {
return String.format("%s %g %d\n", name, cgpa, id);
}
public int compareTo(Student s) {
if (this.cgpa < s.cgpa) return -1;
else if (this.cgpa > s.cgpa) return 1;
else {
int result = -this.name.compareTo(s.name);
if (result != 0) return result;
if (this.id < s.id) return 1;
else if (this.id > s.id) return -1;
return 0;
}
}
}
class Priorities {
private Student[] a;
private int n = 0;
List<Student> getStudents(List<String> events) {
a = new Student[events.size() + 1];
n = 0;
return process(events);
}
private List<Student> process(List<String> events) {
for (String e: events) {
if (e.startsWith("ENTER")) {
String[] data = e.split(" ");
Student st = new Student(Integer.parseInt(data[3]), data[1], Double.parseDouble(data[2]));
insert(st);
} else if (e.equals("SERVED")) {
extract();
} else {
//throw new Exception("Bad instruction: " + e);
}
}
return dumpReverse();
}
private List<Student> dumpReverse() {
List<Student> result = new ArrayList<>();
for (int i = n; i >= 1; i--) {
result.add(a[i]);
}
return result;
}
private void dump() {
System.out.println("Dump ****");
for (int i = 1; i <= n; i++) {
System.out.println(a[i]);
//a[i].toString();
}
System.out.println("End dump ***");
}
private void insert(Student s) {
//a[++n] = s;
int k = ++n;
a[k] = s;
while (k > 1 && s.compareTo(a[k / 2]) <= 0) {
a[k] = a[k/2];
k = k / 2;
}
a[k] = s;
if (n % 2 == 1 && n != 1) {
if (a[n].compareTo(a[n - 1]) < 0)
{
Student tmp = a[n];
a[n] = a[n - 1];
a[n - 1] = tmp;
}
}
//dump();
}
private void extract() {
if (n > 0 ) {
Student s = a[n--];
}
}
}
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());
}
}
}
}