IT/Programmers

[Programmers] 이중우선순위큐

개발자희망생고롸파덕 2024. 3. 20. 16:51

사용언어 : javascript

lv.3

문제풀이 소요 시간 : 5분 21초

문제 유형 : 힙(Heap)

 

#문제

출처 : 프로그래머스

#제출코드

function solution(operations) {
    let queue = [];
    
    operations.map(operator => {
        const [op , num] = operator.split(" ");
        
        if(op === "I"){
            queue.push(+num);
            queue.sort((a, b) => a-b);
        }
        if(op === "D" && queue.length > 0){
            if(num === "-1"){
                queue.shift();
            }else {
                queue.pop();
            }
        }
    })
    
    return queue.length > 0 ? [queue[queue.length-1], queue[0]] : [0,0];
}