Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
Today
Total
관리 메뉴

전공공부

[BOJ_11279] 최대 힙 본문

Study/Problem Solving

[BOJ_11279] 최대 힙

monitor 2023. 10. 30. 06:09

설명


PriorityQueue가 최소 힙이니 이를 반대로 Comparator를 이용해서 내림차순으로 구현해서 풀었다.

 

 

코드


package Data_Structure;
import java.util.*;
import java.io.*;

public class BOJ_11279 {
    public static void main(String[] args) throws Exception{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(in.readLine());
        PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        });
        for(int i = 0; i < N; i++){
            int num = Integer.parseInt(in.readLine());
            if(num == 0){
                if(pq.isEmpty()){
                    System.out.println(0);
                }else{
                    System.out.println(pq.poll());
                }
            }
            else{
                pq.add(num);
            }
        }
    }
}

'Study > Problem Solving' 카테고리의 다른 글

[BOJ_2798] 블랙잭  (1) 2023.11.01
[BOJ_4358] 생태학  (0) 2023.10.31
[BOJ_2504] 괄호의 값  (1) 2023.10.29
[BOJ_1874] 스택 수열  (1) 2023.10.28
[BOJ_14425] 문자열 집합  (1) 2023.10.26