프로그래머스/2레벨
[프로그래머스/C++] 마법의 엘리베이터
Koalitsiya
2024. 3. 8. 23:20
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int elevator(int storey) {
// 현재 층 수가 10층 이하일 때 내려가는게 빠른지 올라가서 10층을 내려가는게 빠른지 판별
if(storey < 10) return min(storey, 11 - storey);
int cntDown = storey % 10;
int cntUp = 10 - storey % 10;
// 현재 층에서 내려가는 횟수와 현재 층에서 올라가는 횟수 중 작은 값을 선택하여 최소 횟수를 계산
return min(elevator((storey - cntDown) / 10) + cntDown, elevator((storey + cntUp) / 10) + cntUp);
}
int solution(int storey) {
return elevator(storey);
}