HR자바 Java Priority Queue

개요[ | ]

HR자바 Java Priority Queue
해커랭크 Java
# 문제 비고
HR자바 Data Structures e
28 HR자바 Java 1D Array
29 HR자바 Java 2D Array
30 HR자바 Java Subarray
31 HR자바 Java Arraylist
32 HR자바 Java 1D Array (Part 2)
33 HR자바 Java List
34 HR자바 Java Map
35 HR자바 Java Stack
36 HR자바 Java Hashset
37 HR자바 Java Generics
38 HR자바 Java Comparator
39 HR자바 Java Sort
40 HR자바 Java Dequeue
41 HR자바 Java BitSet
42 HR자바 Java Priority Queue

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.PriorityQueue;
/*
 * 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 int compareTo(Student s) {
        int c;
        c = Double.valueOf(this.cgpa).compareTo(s.getCgpa());
        if( c != 0 ) return -c;
        c = this.name.compareTo(s.getName());
        if( c != 0 ) return c;
        return s.getId() - this.id;
    }
}

class Priorities {
    public List<Student> getStudents(List<String> events) {
        PriorityQueue<Student> q = new PriorityQueue<Student>();
        for(String event : events){
            if(event.equals("SERVED")) {
                q.poll();
                continue;
            }
            String[] temp = event.split(" ");
            String name = temp[1];
            double cgpa = Double.valueOf(temp[2]);
            int id = Integer.valueOf(temp[3]);
            q.add(new Student(id,name,cgpa));
        }
        List<Student> list = new ArrayList<Student>();
        while( !q.isEmpty() ) {
            list.add( q.remove() );
        }
        return list;
    }
}
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());
            }
        }
    }
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}