개발의변화
프로그래머스 LV2 질문목록 본문
반응형
문제
https://school.programmers.co.kr/learn/courses/30/lessons/42583
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
답
function solution(bridge_length, weight, truck_weights) {
let time = 0;
let bridge = Array.from({length:bridge_length},()=>0)
let totalWeight = 0
while(bridge.length>0) {
time += 1
totalWeight -= bridge.shift()
if (truck_weights.length>0) {
if (totalWeight + truck_weights[0] <= weight) {
totalWeight += truck_weights[0]
bridge.push(truck_weights.shift())
}
else {
bridge.push(0)
}
}
}
return time;
}
다리의 길이만큼 배열을 만들어 직접 다리에서 빼고 넣고를 해서 구현하면 된다
반응형
'알고리즘' 카테고리의 다른 글
프로그래머스 LV2 큰 수 만들기 (0) | 2023.04.13 |
---|---|
프로그래머스 LV2 2 x n 타일링 (0) | 2023.04.13 |
프로그래머스 LV2 프렌즈블록 (0) | 2023.04.11 |
프로그래머스 LV2 파일명 정렬(카카오) (0) | 2023.04.06 |
프로그래머스 LV2 피보나치 수 (0) | 2023.04.02 |