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

잡초·2024년 1월 16일
0
post-thumbnail

문제

풀이

function solution(operations) {
    let arr = []
    let op = operations.map((el) => el.split(" "))
    
    op.forEach((el) => {
        if(el[0] === "I"){
            arr.push(Number(el[1]))
        } else {
            let num = (el[1] === "1" ? Math.max : Math.min)(...arr)
            let idx = arr.indexOf(num)
            arr.splice(idx , 1)
        }
    })
    return arr.length ? [Math.max(...arr),Math.min(...arr)] : [0,0]
}

원래 출제 의도대로라면 힙(Heap)을 사용하여 풀어야 하지만, JS 특성상 힙을 직접 구현해야하기 때문에 다르게 풀었다.

profile
개발자가 되고싶은 잡초

0개의 댓글