https://www.acmicpc.net/problem/1463
코드 :
dp
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static Integer[] dp;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
dp = new Integer[N+1];
dp[0] = dp[1] = 0;
System.out.println(recur(N));
}
public static int recur(int N){
if (dp[N] == null) {
if (N % 6 == 0){
dp[N] = Math.min(recur(N - 1), Math.min(recur(N / 3), recur(N / 2)))+1;
} else if (N % 3 == 0){
dp[N] = Math.min(recur(N / 3), recur(N - 1))+1;
} else if (N % 2 == 0){
dp[N] = Math.min(recur(N / 2), recur(N - 1))+1;
} else
dp[N] = recur(N - 1) + 1;
}
return dp[N];
}
}
참고 :
https://st-lab.tistory.com/133
'알고리즘 > 백준' 카테고리의 다른 글
백준 11727번 - 2xn 타일링2 (Java 8) (0) | 2022.03.22 |
---|---|
백준 11726번 - 2xn 타일링 (Java 8) (0) | 2022.03.22 |
백준 11576번 - Base Conversion (Java 8) (0) | 2022.03.20 |
백준 2745번 - 진법 변환 (Java 8) (0) | 2022.03.19 |
백준 17103번 - 골드바흐 파티션 (Java 8) (0) | 2022.03.19 |
댓글