자바생
Published 2021. 10. 10. 01:10
자바(백준) 11286 절댓값 힙 BOJ(Java)
728x90

오답 노트 & 새로 알게 된 점

해당 문제는 우선순위 큐를 만들 때, comperator를 잘 구현할 수 있는지 물어보는 문제였다.

 


코드

 

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
32
33
34
35
36
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
 
public class Main {
    static int atoi(String str) {
        return Integer.parseInt(str);
    }
 
    static int N;
    static PriorityQueue<Integer> pq = new PriorityQueue<>((o1,o2)->{
        if(Math.abs(o1) == Math.abs(o2))
            return o1 - o2;
        else
            return Math.abs(o1) - Math.abs(o2);
    });
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
 
        N = atoi(br.readLine());
 
        for (int i = 0; i < N; i++) {
            int cmd = atoi(br.readLine());
 
            if(cmd == 0){
                if(pq.isEmpty()) sb.append(0).append("\n");
                else sb.append(pq.poll()).append("\n");
            } else pq.offer(cmd);
        }
 
        System.out.print(sb);
    }
}
 
cs
728x90
profile

자바생

@자바생

틀린 부분이 있다면 댓글 부탁드립니다~😀

검색 태그