11279 최대 힙 (JAVA)

Fekim·2022년 3월 5일
0

ps

목록 보기
29/48
  • 큰 값부터 꺼내기 위해, 우선순위 큐를 생성할때 reversOrder()로 생성
  • sysout으로 각각 출력 시 시간초과되므로, StringBuilder에 결과를 저장하여 마지막에 한번에 출력
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PriorityQueue<Integer> pq =
                new PriorityQueue<>(Collections.reverseOrder());
        StringBuilder sb = new StringBuilder();
        int n = sc.nextInt();
        while(n-->0){
            int cmd = sc.nextInt();
            if(cmd == 0){
                if(pq.isEmpty())
                    sb.append(0).append('\n');
                else
                    sb.append(pq.poll()).append('\n');
            }
            else
                pq.offer(cmd);
        }
        System.out.println(sb);
    }
}
profile
★Bugless 2024★

0개의 댓글