-
문제
programmers.co.kr/learn/courses/30/lessons/42583
코딩테스트 연습 - 다리를 지나는 트럭
트럭 여러 대가 강을 가로지르는 일 차선 다리를 정해진 순으로 건너려 합니다. 모든 트럭이 다리를 건너려면 최소 몇 초가 걸리는지 알아내야 합니다. 트럭은 1초에 1만큼 움직이며, 다리 길이
programmers.co.kr
코드
function solution(bridge_length, weight, truck_weights) { let answer = 0; let onBoard = []; let current_weight = 0; while( truck_weights ) { answer += 1; // 다리가 트럭으로 꽉 차있을 때 현재 무게에서, 나가는 트럭의 무게를 빼준다. if ( onBoard.length === bridge_length ) { current_weight -= onBoard.pop(); } // 들어 갈 공간이 있고, 무게도 남으면 if ( current_weight + truck_weights[0] <= weight ) { let next_truck = truck_weights.shift(); onBoard.unshift(next_truck); current_weight += next_truck; } else { onBoard.unshift(0); } // 다리가 비어있으면, 길이만큼의 시간이 소요 if (truck_weights.length === 0) { answer += bridge_length; break; } } return answer; }
'PS > 프로그래머스' 카테고리의 다른 글
[프로그래머스_Greedy ] 구명보트 ( by using JavaScript ) (0) 2021.04.27 [프로그래머스_정렬 ] 가장 큰 수 ( by using Javascript) (0) 2021.04.22 [프로그래머스_완전탐색] 소수찾기 ( by using JavaScript ) (0) 2021.04.15 [프로그래머스_구현] 모의고사 (0) 2021.04.15 [프로그래머스] 기능개발( 스택큐, by using javscript) (0) 2021.04.06 댓글