in.add()
[42628] 이중우선순위큐 본문
문제 링크 : https://programmers.co.kr/learn/courses/30/lessons/42628
코딩테스트 연습 - 이중우선순위큐
programmers.co.kr
문제 해결 방법
priority queue를 사용하여 구현했다.
import java.util.*;
class Solution {
public int[] solution(String[] operations) {
int[] answer = new int[2];
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(String s : operations) {
String[] op = s.split(" ");
if(op[0].equals("I")) {
pq.add(Integer.parseInt(op[1]));
} else {
if(pq.isEmpty()) {
continue;
}
if(op[1].equals("1")) {
PriorityQueue<Integer> tmp = new PriorityQueue<>();
for(int i = 0; i < pq.size() - 1; i++) {
tmp.add(pq.peek());
pq.remove();
}
pq = tmp;
} else {
pq.remove();
}
}
}
if(pq.isEmpty()) {
return answer;
} else {
int max = pq.peek();
int min = pq.peek();
for(int i : pq) {
max = Math.max(max, i);
min = Math.min(min, i);
}
answer[0] = max;
answer[1] = min;
}
return answer;
}
}'Algorithm > Programmers' 카테고리의 다른 글
| [42579] 베스트 앨범 (0) | 2021.11.08 |
|---|---|
| [84021] 3주차_퍼즐 조각 채우기 (0) | 2021.10.29 |
| [43162] 네트워크 (0) | 2021.10.26 |
| [62048] 멀쩡한 사각형 (0) | 2021.10.25 |
| [1844] 게임 맵 최단거리 (0) | 2021.10.16 |
Comments