Muscardinus

이중우선순위큐 본문

카테고리 없음

이중우선순위큐

Muscardinus 2021. 2. 17. 00:19
728x90

programmers.co.kr/learn/courses/30/lessons/42628?language=javascript

 

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

 

programmers.co.kr

function solution(operations) {
    var answer = [];
    let q = [];
    let index;
    operations.map((operation) => {
        if (operation[0] === "I") {
            let num = +operation.split(" ")[1];
            q.push(num);
        } else if (operation === "D -1") {
            if (!q.length) return;
            index = q.indexOf(Math.min.apply(null, q));
            q.splice(index, 1);
        } else if (operation === "D 1") {
            if (!q.length) return;
            index = q.indexOf(Math.max.apply(null, q));
            q.splice(index, 1);
        }
    })
    if (!q.length) return [0, 0];
    answer = [Math.max.apply(null, q), Math.min.apply(null, q)];
    return answer;
}
728x90
Comments