1927 최소 힙 (JAVA)

Fekim·2022년 3월 5일
0

ps

목록 보기
30/48
  • 작은 값을 우선으로 poll()하기 위해 java.util.PriorityQueue의 기본 생성자로 우선순위 큐를 생성
import java.util.PriorityQueue;
import java.util.Scanner;

/* 1927 최소 힙 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PriorityQueue<Integer> pq =
                new PriorityQueue<>();
        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개의 댓글