Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Tags more
Archives
Today
Total
관리 메뉴

개발의변화

프로그래머스 LV2 질문목록 본문

알고리즘

프로그래머스 LV2 질문목록

refindmySapporo 2023. 4. 12. 19:15
반응형

문제

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

 

프로그래머스

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

programmers.co.kr

 

function solution(bridge_length, weight, truck_weights) {
    let time = 0;
    let bridge = Array.from({length:bridge_length},()=>0)
    let totalWeight = 0
    while(bridge.length>0) {
        time += 1
        totalWeight -= bridge.shift()
        if (truck_weights.length>0) {
            if (totalWeight + truck_weights[0] <= weight) {
                totalWeight += truck_weights[0]
                bridge.push(truck_weights.shift())
            }
            else {
                bridge.push(0)
            }
        }
    }
    return time;
}

 

다리의 길이만큼 배열을 만들어 직접 다리에서 빼고 넣고를 해서 구현하면 된다

반응형