Skip to content

Commit 5b54c98

Browse files
committed
[DP] baekjoon-12852
1 parent 7379760 commit 5b54c98

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@
378378
| 31 | | [SWEA-1952 수영장](src/DP/swea1952) | |
379379
| 32 | | [JUNGOL-1681 해밀턴 순환회로](./src/DP/jo1681) | TSP |
380380
| 33 | | [Baekjoon-2098 외판원 순회](./src/DP/P2098) | TSP |
381+
| 34 | | [Baekjoon-12852 1로 만들기 2](./src/DP/P12852) | |
381382

382383
## Time Complexity
383384

src/DP/P12852/Main.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package DP.P12852;
2+
3+
import java.io.*;
4+
5+
public class Main {
6+
7+
static int N;
8+
static int[] cnt = new int[1000001], memo = new int[1000001];
9+
10+
public static void main(String[] args) throws Exception {
11+
System.setIn(new FileInputStream("src/DP/P12852/input.txt"));
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
14+
N = Integer.parseInt(br.readLine());
15+
for (int i = 2; i <= N; i++) {
16+
cnt[i] = cnt[i-1] + 1;
17+
memo[i] = i-1;
18+
if (i % 3 == 0 && cnt[i] > cnt[i/3] + 1) {
19+
cnt[i] = cnt[i/3] + 1;
20+
memo[i] = i/3;
21+
}
22+
if (i % 2 == 0 && cnt[i] > cnt[i/2] + 1) {
23+
cnt[i] = cnt[i/2] + 1;
24+
memo[i] = i/2;
25+
}
26+
}
27+
28+
StringBuilder sb = new StringBuilder();
29+
sb.append(cnt[N]).append("\n");
30+
while (N >= 1) {
31+
sb.append(N).append(" ");
32+
N = memo[N];
33+
}
34+
System.out.println(sb.toString());
35+
}
36+
}

src/DP/P12852/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [baekjoon-12852] 1로 만들기 2
2+
3+
![image](https://user-images.githubusercontent.com/22045163/112117297-8e487780-8bfe-11eb-8f9b-5516059c6245.png)
4+
5+
![image](https://user-images.githubusercontent.com/22045163/112117342-9ef8ed80-8bfe-11eb-84e7-10e6b908f4c7.png)

src/DP/P12852/input.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10

0 commit comments

Comments
 (0)