반응형
https://school.programmers.co.kr/learn/courses/30/lessons/42628
Solution
import java.util.Collections;
import java.util.PriorityQueue;
class Solution {
public int[] solution(String[] operations) {
int[] answer = {0, 0};
PriorityQueue<Integer> minPq = new PriorityQueue<>();
PriorityQueue<Integer> maxPq = new PriorityQueue<>(Collections.reverseOrder());
for(int i=0; i<operations.length; i++) {
if(operations[i].charAt(0) == 'I') {
operations[i] = operations[i].substring(2);
int num = Integer.parseInt(operations[i]);
maxPq.offer(num);
minPq.offer(num);
}else if(operations[i].charAt(0) == 'D') {
if(maxPq.isEmpty()) continue;
if(operations[i].charAt(2) == '1') {
int maxNum = maxPq.poll();
minPq.remove(maxNum);
}else {
int minNum = minPq.poll();
maxPq.remove(minNum);
}
}
}
if(maxPq.size() == 1) {
answer[0] = maxPq.poll();
answer[1] = answer[0];
}else if(1 < maxPq.size()) {
answer[0] = maxPq.poll();
answer[1] = minPq.poll();
}
return answer;
}
}
반응형
'알고리즘 > 문제풀이' 카테고리의 다른 글
[프로그래머스] 정수삼각형 (0) | 2023.01.01 |
---|---|
Word Break (0) | 2022.09.04 |
[정렬] 문자열 압축 (0) | 2022.08.07 |
[정렬] 오픈채팅방 (0) | 2022.08.07 |
[Heap] 디스크 컨트롤러 (0) | 2022.08.07 |