-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1463.java
More file actions
40 lines (36 loc) · 1014 Bytes
/
1463.java
File metadata and controls
40 lines (36 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.IOException;
import java.util.Scanner;
public class Main {
static int dp[];
static int min(int n){
if(dp[n]==-1){
if(n%3==0&&n%2==0) { //2와 3으로 모두 나뉘어지면
dp[n]=Math.min(Math.min(min(n/3),min(n/2)),min(n-1))+1;
}
else if(n%3==0){//3으로만 나뉘어지면
dp[n]=Math.min(min(n/3),min(n-1))+1;
}
else if(n%2==0){
dp[n]=Math.min(min(n/2),min(n-1))+1;
}
else{
dp[n]=min(n-1)+1;
}
}
return dp[n];
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
dp=new int[n+1];
for(int i=0; i<n+1; i++)
dp[i]=-1;
dp[1]=0;
if(n==1){
System.out.println(dp[1]);
return;
}
dp[2]=1;
System.out.println(min(n));
}
}