문제
다리를 지나는 트럭
문제풀이
function solution(bridge_length, weight, truck_weights) {
var cnt = 1;
var currentWeights = [];
currentWeights.push({"size" : truck_weights.shift(), "time": 1});
while (1) {
var sizeSum = 0;
var shiftTrue = false;
currentWeights.map(v => {
if (v.time != bridge_length) {
sizeSum += v.size;
}
else {
shiftTrue = !shiftTrue;
}
v.time += 1;
});
if (shiftTrue) currentWeights.shift();
if (truck_weights.length != 0) {
if (weight >= (sizeSum + truck_weights[0])) {
currentWeights.push({"size" : truck_weights.shift(), "time": 1});
}
}
else {
if (currentWeights.length == 0) {
cnt++;
break;
}
}
cnt++;
}
return cnt;
}