# [힙][Lv3] 이중우선순위큐 - 자바스크립트 js

# 프로그래머스 문제

https://programmers.co.kr/learn/courses/30/lessons/42628 (opens new window)

# 시도 1.

처음엔 힙의 구조와 원리를 이용해서 푸려고 힙을 만들다가 문득 굳이.. 이렇게 해야할까? 라는 생각이 들어서 하던건 킵하고 그냥 바로 생각나는대로 코딩했는데 풀렸다

레벨3문제라 함정이나 생각 못한 예외사항이 있어서 채점할 때 하나쯤은 에러날 줄 알았음..

function solution(operations) {
    var answer = [];
    let arr = [];
    
    for(var i=0; i<operations.length; i++){
        var operation = operations[i].split(' ');
        
        if(operation[0]==='I'){
            arr.push(parseInt(operation[1]));
        } else if(operation[1]==='1') {
            arr.pop();
        } else if(operation[1]==='-1') {
            arr.shift();           
        } else {
            console.log('error');
        }
        
        arr.sort((a,b) => a-b);
    }
    
    if(arr.length === 0) answer = [0,0];
    else answer = [arr.pop(), arr.shift()];
    
    return answer;
}
채점을 시작합니다.
정확성  테스트
테스트 1 〉	통과 (0.10ms, 29.9MB)
테스트 2 〉	통과 (0.16ms, 30.1MB)
테스트 3 〉	통과 (0.14ms, 29.8MB)
테스트 4 〉	통과 (0.12ms, 30.1MB)
테스트 5 〉	통과 (0.10ms, 30MB)
테스트 6 〉	통과 (0.09ms, 30MB)
채점 결과
정확성: 100.0
합계: 100.0 / 100.0

→ 나중에 힙 방식으로 구현해보고 추가 하기!

Last Updated: 8/11/2022, 5:17:02 AM