https://school.programmers.co.kr/learn/courses/30/lessons/148653
- 1 ≤ storey ≤ 100,000,000
class Solution {
public int solution(int storey) {
// 자리별로 숫자 가져오기 용이하게 String을 쓴다.
String num = String.valueOf(storey);
return helper(num, num.length()-1, 0, 0);
}
public int helper(String num, int idx, int cnt, int up) {
// 마지막 자리 넘어서면 올림이랑 이제까지 카운트한 값 리턴
if(idx<0) {
return cnt+up;
}
// 현재 자리 수 + 올림 값
int n = (num.charAt(idx)-'0')+up;
// 10일 때는 그냥 올림 추가해서 다음자리로 넘김(최소만 찾으면 되므로)
if(n==10) {
return helper(num, idx-1, cnt, 1);
}
// 현재 자리 수 +와 -일 때 중 Minimum 값을 리턴
return Math.min(helper(num, idx-1, cnt+10-n, 1),helper(num, idx-1, cnt+n, 0));
}
}