프로그래머스/Level2
프로그래머스#5(Lv.2)_다리를 지나는 트럭
개발자_조이킴
2021. 11. 25. 23:47
다리를 지나는 트럭
오랜만에 que와 stack 개념에 대해서 다시 살펴보게된 문제!
다른 분들이 하신것 보고 배우고 또 배우자! (자료구조 큐 개념 복습!)
// 프로그래머스 - 다리를 지나는 트럭 function solution(bridge_length, weight, truck_weights) { let head = truck_weights.shift() let step = [head] let timeList = [1] let time = 1 let sum = step.reduce((acr, cur) => acr + cur, 0) while(truck_weights.length !== 0 || sum !== 0) { timeList = timeList.map((el) => el + 1) // 만약 다 건너가면 삭제해준다 if(timeList[0] > bridge_length) { step.splice(0, 1) timeList.splice(0, 1) } // step 하나씩 오면서 let next = 0 if(truck_weights) next = truck_weights[0] sum = step.reduce((acr, cur) => acr + cur, 0) + next if(sum <= weight) { // 넣어준다 step.push(next) // 지워준다 truck_weights.splice(0, 1) // time도 하나 넣어준다 timeList.push(1) } sum = step.reduce((acr, cur) => acr + cur, 0) time++ } return time } |
<다른분의 solution>
![]() |