![[Java] 백준 1927번 문제 (최소 힙)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FBCVAI%2FbtsyTEbYWaS%2FeJ6POwzuHr4GG9VgebkrN1%2Fimg.png)
[Java] 백준 1927번 문제 (최소 힙)자료구조 & 알고리즘/BOJ2023. 10. 22. 03:06
Table of Contents
문제설명
소스코드
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
public class Main
{
public static void main(String[] args) throws Exception
{
PriorityQueue<Integer> maxHeap = new PriorityQueue<>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(br.readLine());
for(int i = 0; i < N; ++i)
{
int input = Integer.parseInt(br.readLine());
if(input == 0)
{
if(maxHeap.peek() == null) sb.append(0).append("\n");
else sb.append(maxHeap.poll()).append("\n");
}
else maxHeap.add(input);
}
System.out.println(sb.toString());
}
}
설명
2023.10.22 - [Java Category/Java] - [Java] Heap(힙)과 Priority Queue(우선순위 큐)
[Java] Heap(힙)과 Priority Queue(우선순위 큐)
힙과 우선순위 큐 결론부터 말하자면, 우선순위 큐는 ADT(Abstract Data Type) 이고, 힙은 우선순위 큐의 개념을 구현한 것이다. ADT 구현하고자 하는 구조에 대해 구현 방법은 명시하지 않고 자료구조
rebugs.tistory.com
'자료구조 & 알고리즘 > BOJ' 카테고리의 다른 글
[Java] 백준 11286번 문제 (절댓값 힙) (0) | 2023.10.23 |
---|---|
[Java] 백준 11279번 문제 (최대 힙) (0) | 2023.10.22 |
[Java] 백준 1673번 문제 (치킨 쿠폰) (0) | 2023.10.19 |
[Java] 백준 1918번 문제 (후위 표기식) (0) | 2023.10.18 |
[Java] 백준 1935번 문제 (후위 표기식2) (0) | 2023.10.17 |