티스토리 뷰
이 문제는 다이나믹 프로그래밍을 이용하여 해결할 수 있다.
i번째 값을 만들 수 있는 경우는
i-1 번째에서 1을 더하는 경우
i%2 == 0인 경우에서 1을 더하는 경우
i%3 == 0인 경우에서 1을 더하는 경우이다.
이 세가지 경우를 for 문을 이용하여 반복하여 해결한다.
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; | |
public class Problem1463 { | |
static int[] d = new int[100]; | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int N = sc.nextInt(); | |
sc.close(); | |
int result = one(N); | |
System.out.println(result); | |
} | |
public static int one(int N) { | |
int[] dp = new int[N + 1]; | |
dp[1] = 0; | |
for (int i = 2; i <= N; i++) { | |
// i-1 번째에 1을 더하는 경우 | |
dp[i] = dp[i - 1] + 1; | |
// i%2 == 0인 경우에서 1을 더하는 경우 | |
if (i % 2 == 0 && dp[i] > dp[i / 2] + 1) { | |
dp[i] = dp[i / 2] + 1; | |
} | |
// i%3 == 0인 경우에서 1을 더하는 경우 | |
if (i % 3 == 0 && dp[i] > dp[i / 3] + 1) { | |
dp[i] = dp[i / 3] + 1; | |
} | |
} | |
return dp[N]; | |
} | |
} |
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[백준][DP] 5721 사탕줍기 대회 (0) | 2017.10.26 |
---|---|
[백준] 1504 특정한 최단 경로 (0) | 2017.10.25 |
[백준][세그먼트 트리] 1275 커피숍2 (0) | 2017.10.25 |
[백준] 1753 다익스트라 알고리즘 (0) | 2017.10.12 |
[백준][다이나믹] 2533 사회망 서비스 (0) | 2017.09.15 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 하둡
- 파이썬
- S3
- Tez
- Hadoop
- bash
- 하이브
- error
- nodejs
- emr
- AWS
- java
- Python
- 알고리즘
- SPARK
- oozie
- 정올
- 오류
- HDFS
- SQL
- 다이나믹
- airflow
- ubuntu
- mysql
- Linux
- yarn
- hbase
- HIVE
- build
- 백준
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함