https://www.acmicpc.net/problem/1463
1463번: 1로 만들기
첫째 줄에 1보다 크거나 같고, 106보다 작거나 같은 정수 N이 주어진다.
www.acmicpc.net
코드 :
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
[백준] 1463번 : 1로 만들기 - JAVA [자바]
www.acmicpc.net/problem/1463 1463번: 1로 만들기 첫째 줄에 1보다 크거나 같고, 106보다 작거나 같은 정수 N이 주어진다. www.acmicpc.net 문제 알고리즘 [접근 방법] 생각보다 어렵지 않게 풀 수 있는 문제다...
st-lab.tistory.com
'알고리즘 > 백준' 카테고리의 다른 글
백준 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 |
댓글