프로그래머스 | 다리를 지나는 트럭

커몽·2021년 6월 28일
0

프로그래머스 level2

목록 보기
17/38
post-custom-banner
function solution(bridge_length, weight, truck_weights) {
  var answer = 0;
  let bridge = new Array(bridge_length).fill(0);
  let count = 0;
  let now = truck_weights.shift();
  count += now;
  bridge.push(now);
  bridge.shift();
  answer++;

  while (count) {
    count -= bridge.shift();
    now = truck_weights.shift();
    if (count + now <= weight) {
      count += now;
      bridge.push(now);
    } else {
      truck_weights.unshift(now);
      bridge.push(0)
    }
    answer++
  }

  return answer;
}
post-custom-banner

0개의 댓글