99클럽 코테 스터디 19일차 TIL + 힙
2024. 11. 15. 15:02ㆍAlgorithm Problem Solving
https://www.acmicpc.net/problem/1927
1. 알고리즘! 생각해보자
1) 힙(Priority Queue)를 사용하자
2) 0일 때, 비어있으면 0을, 비어있지 않으면 poll 출력
3) 0이 아닐 때, add queue
2. 해결 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
//Silver_1927.java
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());// 1~n
int input;
PriorityQueue<Integer> minQueue = new PriorityQueue<Integer>();
for (int i = 0; i < n; i++) {
input = Integer.parseInt(br.readLine());
if (input>0) {
minQueue.add(input);
} else {
if (!minQueue.isEmpty()) {
System.out.println(minQueue.poll());
}
else{
System.out.println(0);
}
}
}
br.close();
}
}
3. 레퍼런스
[백준 1927] 우선순위 큐 java(자바) - 최소 힙
최소 힙 문제 널리 잘 알려진 자료구조 중 최소 힙이 있다. 최소 힙을 이용하여 다음과 같은 연산을 지원하는 프로그램을 작성하시오. 배열에 자연수 x를 넣는다. 배열에서 가장 작은 값을 출력
konkukcodekat.tistory.com
'Algorithm Problem Solving' 카테고리의 다른 글
99클럽 코테 스터디 24일차 TIL + 힙 (1) | 2024.11.20 |
---|---|
99클럽 코테 스터디 20일차 TIL + 힙 (0) | 2024.11.16 |
99클럽 코테 스터디 18일차 TIL + 스택/큐 (1) | 2024.11.14 |
99클럽 코테 스터디 17일차 TIL + 스택/큐 (1) | 2024.11.13 |
99클럽 코테 스터디 15일차 TIL + 스택/큐 (2) | 2024.11.11 |