https://programmers.co.kr/learn/courses/30/lessons/42583
function solution(bridge_length, weight, truck_weights) {
let answer = 0;
// 변수설정
let onBridge = [];
let truckWait = truck_weights;
let sec = 0; // 시간
let totalWeight = 0; // 다리 위 무게
// 다리를 queue로 설정
for(let i=0; i<bridge_length; i++){
onBridge.push(0);
}
//처음 트럭 진입
let truckNow = truckWait.shift();
onBridge.shift();
onBridge.push(truckNow);
totalWeight = totalWeight + truckNow;
sec = sec + 1;
//이동시 고려사항 - 시간, 무게
while(totalWeight) {
let truckGone = onBridge.shift();
totalWeight = totalWeight - truckGone;
truckNow = truck_weights.shift();
if (totalWeight + truckNow <= weight) {
onBridge.push(truckNow);
totalWeight = totalWeight + truckNow;
} else {
onBridge.push(0);
truckWait.unshift(truckNow);
}
sec = sec + 1;
}
answer = sec;
return answer;
}
변수 설정 let // var에 따라서 결과가 달랐음..