ALGORITHM/PROGRAMMERS

[프로그래머스] 이중우선순위큐

0298 2021. 8. 11. 13:53

https://programmers.co.kr/learn/courses/30/lessons/42628

 

코딩테스트 연습 - 이중우선순위큐

 

programmers.co.kr

2021-08-11


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.*;
 
class Solution {
    public int[] solution(String[] operations) {
         PriorityQueue<Integer> pq = new PriorityQueue<>();
 
        for(int i = 0; i < operations.length; i++) {
            String key = operations[i].split(" ")[0];
            int value = Integer.parseInt(operations[i].split(" ")[1]);
            if(key.equals("I")) {
                pq.add(value);
            } else {
                if(!pq.isEmpty()) {
                    if(value == 1)pq.remove(pq.stream().mapToInt(x -> x).max().getAsInt());
                    else pq.poll();
                }
            }
        }
        if(pq.size() == 0return new int[]{00};
        return new int[]{pq.stream().mapToInt(x -> x).max().getAsInt(), pq.peek()};
    }
}
cs

#문제풀이

priorityQueue 하나로 풀었다.

pq.stream().mapToInt(x -> x).max().getAsInt()로 최댓값 찾고, peek()으로 최솟값 찾았다.

 

다른 사람들 푼 방식을 보니깐 max 버젼 min 버젼 두 개로 만들어서 푼 사람들도 있었다.