PriorityQueue를 사용했다.
오름차순으로 우선순위가 되는 PriorityQueue를 선언하고 0이 입력되면 PriorityQueue가 비어있는지 확인 후 출력한다.
0이 아닌 수가 입력되면 PriorityQueue에 add 해 준다.
결과 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PriorityQueue<Integer> list = new PriorityQueue<>();
int n = Integer.parseInt(br.readLine());
int num;
for(int i = 0; i<n; i++)
{
num = Integer.parseInt(br.readLine());
if(num == 0)
{
System.out.print(list.isEmpty() ? "0\n" : list.poll()+"\n");
}
else
{
list.add(num);
}
}
}
}
'백준' 카테고리의 다른 글
5052번:전화번호 목록(Java) (0) | 2023.02.24 |
---|---|
5430번:AC(Java) (0) | 2023.02.24 |
11286번:절댓값 힙(Java) (0) | 2023.02.20 |
1302번: 베스트셀러(Java) (0) | 2023.02.19 |
1158번: 요세푸스 문제(Java) (0) | 2023.02.16 |