We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Java Priority Queue
Java Priority Queue
Sort by
recency
|
273 Discussions
|
Please Login in order to post a comment
class Student { private int id; private String name; private double cgpa;
}
class Priorities { public List getStudents(List events) {
class Student { private int id; private String name; private double cgpa;
}
class Priorities { public List getStudents(List events) {
}
Nice problem — it really checks whether you understand how ordering works inside a priority queue rather than just using it blindly. Telugu 365 ID Login
import java.util.ArrayList; import java.util.List; import java.util.Scanner;
import java.util.Comparator; import java.util.PriorityQueue; import java.util.Scanner; import java.util.*; class Student implements Comparable{ String name = new String(); double cgpa; int id; public Student(String name, double cgpa,int id){ this.name = name; this.cgpa =cgpa; this.id = id; } public String getName(){ return this.name; } public int compareTo(Student s) { if(cgpa == s.cgpa) { if(name.compareTo(s.name) == 0) { if(id == s.id) return 0; else if (id > s.id) return 1; else return -1; } else return name.compareTo(s.name); } else if(cgpa > s.cgpa) return -1; else return 1; } }
class Priorities{ public ArrayList getStudents(List events) { int n = events.size(); PriorityQueue pq = new PriorityQueue(); for(String i:events) { String[] s = new String[4]; s = i.split("\s+"); if(s.length>1) { pq.add(new Student(s[1],Double.valueOf(s[2]),Integer.valueOf(s[3]))); } else { pq.poll(); } } while(pq.size()>1) { System.out.println(pq.poll().name); } return new ArrayList(pq); } }
public class Solution { private final static Scanner scan = new Scanner(System.in); private final static Priorities priorities = new Priorities();
}