본문 바로가기
Study or Lecture/항해 99클럽 코딩테스트 스터디

항해 99클럽 코딩테스트 스터디 19일차 TIL (자바 / 비기너)

by yoondoo 2024. 11. 15.
728x90

<오늘의 학습 키워드>

- 우선순위 큐

<오늘의 문제>

제목 : 백준 / 실버2 / 1927번 최소힙

https://www.acmicpc.net/problem/1927

<풀이>

정답은 더보기

더보기
package BOJ.자료구조.최소힙_1927.insub2004_241115;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.PriorityQueue;

public class Main {

    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());

        PriorityQueue<Integer> queue = new PriorityQueue<>();

        for (int i = 0; i < N; i++) {
            int input = Integer.parseInt(br.readLine());
            if (input == 0) {
                if (queue.isEmpty()) {
                    System.out.println(0);
                } else {
                    System.out.println(queue.poll());
                }
            } else {
                queue.add(input);
            }
        }

        br.close();
    }
}

 

- 큐에 값을 넣을 때 정렬을 유지해주는 우선순위큐의 특징을 이용했다.

<결론, 주의할점>

 

반응형

댓글