https://school.programmers.co.kr/learn/courses/30/lessons/42628
이중 우선순위 큐는 다음 연산을 할 수 있는 자료구조를 말합니다.
이중 우선순위 큐가 할 연산 operations가 매개변수로 주어질 때, 모든 연산을 처리한 후 큐가 비어있으면 [0,0] 비어있지 않으면 [최댓값, 최솟값]을 return 하도록 solution 함수를 구현해주세요.
- operations는 길이가 1 이상 1,000,000 이하인 문자열 배열입니다.
- operations의 원소는 큐가 수행할 연산을 나타냅니다.
- 원소는 “명령어 데이터” 형식으로 주어집니다.- 최댓값/최솟값을 삭제하는 연산에서 최댓값/최솟값이 둘 이상인 경우, 하나만 삭제합니다.
- 빈 큐에 데이터를 삭제하라는 연산이 주어질 경우, 해당 연산은 무시합니다.
import java.util.*;
class Solution {
static TreeMap<Integer, Integer> map;
public static int[] solution(String[] operations) {
map = new TreeMap<Integer, Integer>();
for(String operation : operations) {
String[] oper = operation.split(" ");
int num = Integer.parseInt(oper[1]);
if(oper[0].equals("D")) {
if(map.size() == 0) continue;
delete(num);
} else if(oper[0].equals("I")) {
insert(num);
}
}
return map.size() == 0 ? new int[] {0, 0} : new int[] {map.lastKey(), map.firstKey()};
}
static void insert(int num) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
static void delete(int num) {
int key = num == 1 ? map.lastKey() : map.firstKey();
if(map.put(key, map.get(key) - 1) == 1) map.remove(key);
}
}