📝 문제
🔑 풀이 과정
· 우선순위 큐 클래스를 사용한 가장 기본적인 문제. 삽입, 추가를 학습할 수 있는 문제.
🔓 답안
import java.io.*;
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(br.readLine()); //연산의 개수
PriorityQueue<Integer> queue = new PriorityQueue<>();
for(int i = 0; i < N; i++){
int x = Integer.parseInt(br.readLine());
if(x > 0){ //x가 자연수라면 배열에 x값을 넣는 연산
queue.add(x);
}
else if(x == 0){ //x가 0이라면
if(!queue.isEmpty()){ //배열에서 가장 작은 값을 출력하고 그 값을 배열에서 제거
int min = queue.remove();
sb.append(min).append("\n");
}
else{ //만약 배열이 비어있다면 0을 출력
sb.append(0).append("\n");
}
}
}
bw.write(sb.toString());
bw.flush();
bw.close();
}
}
🖤 알고리즘 분류
- 자료 구조
- 우선순위 큐
'PS > Baekjoon' 카테고리의 다른 글
[Baekjoon] 11286 - 절댓값 힙 (0) | 2023.09.01 |
---|---|
[Baekjoon] 11279 - 최대 힙 (0) | 2023.08.31 |
[Baekjoon] 2805 - 나무 자르기 (0) | 2023.08.29 |
[Baekjoon] 25314 - 코딩은 체육과목 입니다 (0) | 2023.08.28 |
[Baekjoon] 10101 - 삼각형 외우기 (0) | 2023.08.27 |
댓글