백준 11279번 - 최대 힙(silver 2)

dropKick·2020년 7월 7일
0

코딩테스트

목록 보기
5/17

풀이

  • 문제 이름 자체가 최대 힙인 최대 힙 문제
    힙을 알고 있고, 컬렉션을 알고 있다면 간단한 구현 문제
    다만 컬렉션 우선순위 큐는 Natrural Order라서 최대 힙을 구현하려면 reverseOrder해주어야 한다.
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int input = sc.nextInt();
        int data = 0;
        if (input != 0 && input < 100001) {
            PriorityQueue<Integer> pq = new PriorityQueue<>(1000000, Collections.reverseOrder());
            while (input != 0) {
                data = sc.nextInt();
                if (data != 0) {
                    pq.add(data);
                } else {
                    if (pq.peek() == null) {
                        System.out.println(0);
                    } else {
                        System.out.println(pq.poll());
                    }
                }
                input--;
            }
        }
    }

0개의 댓글