[백준 11286] 절대값 힙 구현(우선순위 큐-중요)
- 💾 알고리즘/LeetCode_백준
- 2023. 6. 5. 09:05
🔗 문제
https://www.acmicpc.net/problem/11286
💡 힌트
우선순위 큐 이론 참조
Java 우선순위 큐(Priority Queue) 와 Comparable, Comparator – Jihun's Development Blog (cjh5414.github.io)
💻 코드
package org.kyhslam.DoItAlgorithm.ch02;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
public class p11286 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
PriorityQueue<Integer> myQueue = new PriorityQueue<>( (o1, o2) -> {
int first_abs = Math.abs(o1);
int second_abs = Math.abs(o2);
if (first_abs == second_abs) {
return o1 > o2 ? 1 : -1;
} else {
return first_abs - second_abs;
}
});
for (int i = 0; i < N; i++) {
int request = Integer.parseInt(br.readLine());
if (request == 0) {
if (myQueue.isEmpty()) {
System.out.println("0");
} else {
System.out.println(myQueue.poll());
}
} else {
myQueue.add(request);
}
}
}
}
'💾 알고리즘 > LeetCode_백준' 카테고리의 다른 글
[백준 17298] 오큰수 (스택) (0) | 2023.05.30 |
---|---|
[백준 11003] 최솟값 찾기 (슬라이딩 윈도우) (0) | 2023.05.25 |
[백준 12891] 좋은 수 구하기(슬라이딩 윈도우) (0) | 2023.05.18 |
[백준 1253] 좋은 수 구하기(투 포인터) (0) | 2023.05.18 |
[백준 1940] 주몽의 명령(투 포인터) (0) | 2023.05.18 |