Created
April 12, 2019 17:49
-
-
Save nvmnghia/9f5ae65972a664e8cd7df9ed00e97212 to your computer and use it in GitHub Desktop.
Convert to ArrayList by manual polling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.PriorityQueue; | |
class Student implements Comparable<Student> { | |
private int id; | |
private String name; | |
private double cgpa; | |
public Student(String name, double cgpa, int id) { | |
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) { | |
if (this.cgpa != o.cgpa) { | |
return this.cgpa > o.cgpa ? -1 : 1; | |
} | |
int compareName = this.name.compareTo(o.name); | |
if (compareName != 0) { | |
return compareName; | |
} | |
return this.id - o.id; | |
} | |
} | |
class Priorities { | |
public List<Student> getStudents(List<String> events) { | |
PriorityQueue<Student> pQueue = new PriorityQueue<>(); | |
for (String event : events) { | |
if (event.charAt(0) == 'E') { | |
String[] ops = event.split(" "); | |
pQueue.offer(new Student(ops[1], Double.parseDouble(ops[2]), Integer.parseInt(ops[3]))); | |
} else { | |
pQueue.poll(); | |
} | |
} | |
List<Student> students = new ArrayList<>(); | |
while (pQueue.size() > 0) { | |
students.add(pQueue.poll()); | |
} | |
return students; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment