06.10에 푼 문제입니다🌷
queue 문제이다!
function solution(bridge_length, weight, truck_weights) {
var answer = 1;
//queue는 다리를 건너는 트럭들이 있다. [트럭 무게, 트럭이 다리에 있는 시간]
const queue=[[truck_weights.shift(),1]]
let totaltrucks=queue[0][0]
//다리 위에 트럭이 없으면 모든 트럭이 건넌 것이다.
while(queue.length!==0){
answer++
for(let q of queue){
q[1]+=1
}
//앞의 트럭이 다리 길이만큼 다 지나가면 다리 위 트럭 무게에서 빼주고 queue에서도 빼준다
if(queue[0][1]>bridge_length) {
totaltrucks-=queue[0][0]
queue.shift()
}
// 다리에 더 올라올 수 있으면 다리로 올린다.
if(truck_weights[0]+totaltrucks<=weight){
totaltrucks+=truck_weights[0]
queue.push([truck_weights.shift(),1])
}
}
return answer;
}