"Life is Full of Possibilities" - Soul, 2020

알고리즘

[알고리즘] 프로그래머스 42628 이중우선순위큐 자바스크립트

m2ndy 2024. 1. 28. 11:34

 

 

 

 

 

function solution(operations) {
    let arr = [];
    for (let i=0; i < operations.length; i++) {
        const word = operations[i].split(' ')[0]
        const num = operations[i].split(' ')[1]
        if (word === 'I') {
            arr.push(num)
            arr.sort((a, b) => a - b);
            continue
        }
        if (num === '-1') { // 최솟값 삭제
            arr.shift()
            continue
        }
        arr.pop() // 최댓값 삭제
    }
    if (arr.length > 0) {
        return [Number(arr[arr.length-1]), Number(arr[0])]
    }
    return [0, 0]
}