다리를 지나는 트럭

const solution = (bridge_length, weight, truck_weights) => {
  const bridge = new Array(bridge_length).fill(0);
  let weightOnBridge = 0;
  let count = 0;
  do {
    const shifted = bridge.shift();
    const expect = weightOnBridge - shifted + truck_weights[0];
    if (expect <= weight) {
      bridge.push(truck_weights.shift());
      weightOnBridge = expect;
    } else {
      bridge.push(0);
      weightOnBridge -= shifted;
    }
    count++;
  } while (weightOnBridge > 0);
  return count;
};
  • do while 처음 써봤다

0개의 댓글