본문 바로가기
Coding Test/Programmers

[Programmers] (스택/큐) Lv 2. 다리를 지나는 트럭

by song.ift 2023. 2. 24.

https://school.programmers.co.kr/learn/courses/30/lessons/42583

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

function solution(bridge_length, weight, truck_weights) {
    if (truck_weights.length === 1) return bridge_length + 1;

    var sec = 1;
    let status = [];

    while (truck_weights.length || status.length) {
        let curWeight = 0;
        status = status.reduce((acc, t, i) => {
            if (i === 0)
                ++sec;

            if (++t.time <= bridge_length) {
                acc.push(t);
                curWeight += t.weight;
            }
            return acc;
        }, []);


        if (curWeight + truck_weights[0] <= weight) status.push({ weight: truck_weights.shift(), time: 1 });
    }
    return sec;
}

GitHub : https://github.com/developeSHG/Algorithm-Baekjoon_Programmers/tree/main/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/lv2/42583.%E2%80%85%EB%8B%A4%EB%A6%AC%EB%A5%BC%E2%80%85%EC%A7%80%EB%82%98%EB%8A%94%E2%80%85%ED%8A%B8%EB%9F%AD

 

GitHub - developeSHG/Algorithm-Baekjoon_Programmers: 백준 and 프로그래머스 소스코드

백준 and 프로그래머스 소스코드. Contribute to developeSHG/Algorithm-Baekjoon_Programmers development by creating an account on GitHub.

github.com

 

댓글