Jun 개발노트

프로그래머스 - 다리를지나는트럭

February 24, 2020

다리를지나는트럭

const passBrigth = (bridge_length, weight, trucks) => {
    let count = 0;
    let currentWeight = 0;
    const bridge = Array(bridge_length).fill(0);
    while (currentWeight || trucks.length) {
        let passTruck = bridge.pop();
        currentWeight -= passTruck;
        let next = 0;
        if (currentWeight + trucks[0] <= weight) {
            next = trucks.shift();
            currentWeight += next;
        }
        bridge.unshift(next);
        count++;
    }

    return count;
};

test("다리를 지나는 트럭", () => {
    // expect(passBrigth(2,10,[7,4,5,6])).toEqual(8);
    expect(passBrigth(100, 100, [10])).toBe(101);
});

Written by Junho You 배운것을 기록하자