[프로그래머스] 점프와 순간이동

2020. 4. 13. 12:19프로그래머스/LEVEL 2

import java.util.*;

public class Solution {
    public int solution(int n) {
        int ans = 1;

        while(n!=1){
            if(n%2==0){
                n=n/2;
            }else{
                ans++;
                n=n-1;
            }
        }

        return ans;
    }
}

도착점이 짝수이면 전에 도착한데까지에서 순간이동을 하면 되므로 소모되는 것이없다.

도착점이 홀수이면 (도착점-1) 에서 1만큼 소모하면 되므로 (도착점-1) 까지의 소모값에 1을 더해주면 된다.