프로그래머스 #4

노태경·2021년 6월 29일
0

프로그래머스

목록 보기
4/11

스택/큐 - 다리를 지나는 트럭

코플릿에서 풀었던 프린트 문제구나!

function solution(bridge_length, weight, truck_weights) {
    const queue = new Array(bridge_length).fill(0);
    const enqueue = (el) => queue.push(el);
    const dequeue = () => queue.shift();
    let second = 0;
    let curWeight = 0;
    
    do {
        second++;
        curWeight -= dequeue();
        if(truck_weights[0] + curWeight <= weight){
            curWeight += truck_weights[0];
            enqueue(truck_weights.shift());
        } else {
            enqueue(0);
        }
    } while(curWeight > 0)
    
    return second;
}


다소 비효율적일 수 있겠다고 생각 중 다른 사람들의 풀이를 보니

중간에 무게를 초과하는 경우에 시간만 증가하는 부분을 상당히 단축시킨 경우들을 보았다. 대단한 사람들..

profile
개발자 공부 일기😉

0개의 댓글

관련 채용 정보