Created
January 19, 2019 14:49
-
-
Save jayden-lee/491d929a2b2b1e787af258a30fa15dd7 to your computer and use it in GitHub Desktop.
백준알고리즘 1463번: 1로 만들기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
/** | |
* 1로 만들기 문제<br> | |
* 알고리즘 분류 : DP | |
* | |
* @author jayden-lee | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
int n = scanner.nextInt(); | |
int[] dp = new int[n + 1]; | |
for (int i = 2; i <= n; i++) { | |
dp[i] = dp[i - 1] + 1; | |
if (i % 3 == 0) { | |
dp[i] = Math.min(dp[i], dp[i / 3] + 1); | |
} | |
if (i % 2 == 0) { | |
dp[i] = Math.min(dp[i], dp[i / 2] + 1); | |
} | |
} | |
System.out.println(dp[n]); | |
scanner.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment