🖥 나의 JS 코드
첫번째 시도
function solution(bridge_length, weight, truck_weights) {
var time = 0;
let on_truck = [], time_truck = [];
while(on_truck.length != 0 || truck_weights.length != 0){
if(time_truck.length != 0){
time_truck = time_truck.map((time) => time +=1);
}
if(time_truck[0] === bridge_length){
on_truck.shift();
time_truck.shift();
}
let sum = on_truck.reduce((acc, cur)=>{
return acc + cur
},0)
if(sum + truck_weights[0] <= weight && truck_weights.length != 0){
on_truck.push(truck_weights.shift());
time_truck.push(0);
}
time++;
}
return time;
}
두번째 시도
function solution(bridge_length, weight, truck_weights) {
let onBridge = [];
let onBridgeTime = [];
let time = 1;
let sum = truck_weights[0]
onBridge.push(truck_weights.shift())
onBridgeTime.push(1);
while(onBridge.length != 0){
if(onBridgeTime[0] === bridge_length){
sum -= onBridge[0];
onBridge.shift();
onBridgeTime.shift();
}
onBridgeTime = onBridgeTime.map((data)=> data+1)
time++;
if(sum + truck_weights[0] <= weight
&& onBridge.length < bridge_length){
sum += truck_weights[0];
onBridge.push(truck_weights.shift())
onBridgeTime.push(1);
}
}
return time;
}