지난 풀이 참고
weight
값과 position
값을 갖게 했다.forEach
문 안에서 하려다가 시간을 많이 잡아먹었다.function solution(bridge_length, weight, truck_weights) {
const trucks = truck_weights.map((truck) => {
return {weight: truck, pos : 1};
})
let currentWeight = 0;
let time = 0;
let passing = [];
while(trucks.length > 0 || passing.length > 0) {
time++
passing.forEach((truck) => {
truck.pos++;
});
if(passing.length > 0) {
if(passing[0].pos > bridge_length) {
currentWeight -= passing[0].weight;
passing.shift();
}
}
if(trucks.length > 0) {
if(currentWeight + trucks[0].weight <= weight) {
currentWeight += trucks[0].weight;
passing.push(trucks.shift());
}
}
}
return time;
}