DP로 풀었다. 

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());

        
        int N = Integer.parseInt(st.nextToken());
        

        long dp[] = new long[N+1];
        
        dp[0] = Long.parseLong("0");
        dp[1] = Long.parseLong("0");
        
        for(int i=2; i<=N; i++) {
//        	System.out.println("N=="+N+",i=="+i);
        	if (i%3 == 0) {
//        		System.out.println(i+"::mod3");
        		if (dp[i]==0 || (dp[i] >1+dp[i/3])) { 	
        			dp[i] = 1+dp[i/3];
        		}
        	}
        	if (i%2 == 0) {
//        		System.out.println(i+"::mod2");
        		if (dp[i]==0 || (dp[i] >1+dp[i/2])) {
//        			System.out.println(i/2+">"+dp[i/2]);
        			dp[i] = 1+dp[i/2]; 
        		}
        	}
        	
        	if((i%3 != 0 && i%2 != 0) || dp[i] > dp[i-1]+1) {
//        		System.out.println(i+"::1+"+dp[i-1]);
        		dp[i] = dp[i-1]+1;
        	}
        	
//        	System.out.println(i+"="+dp[i]);
        }
        
        
        System.out.println(dp[N]);
		
	}

}

'알고리즘' 카테고리의 다른 글

JAVA 백준 1912 연속합  (0) 2020.02.11
JAVA 백준 1000번 A+B  (0) 2020.02.06