Skip to content

Commit 1e12314

Browse files
authored
[20251121] PGM / Lv2 / 숫자 변환하기 / 이종환
1 parent 7e7fbc5 commit 1e12314

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
class Solution {
6+
public int solution(int x, int y, int n) {
7+
int[] dp = new int[1_000_001];
8+
Arrays.fill(dp,Integer.MAX_VALUE/2);
9+
dp[x] = 0;
10+
11+
for (int i = x; i <= y; i++){
12+
if (dp[i] >= Integer.MAX_VALUE/2) continue;
13+
int next = i+n;
14+
int mul2 = i*2;
15+
int mul3 = i*3;
16+
17+
if ( next <= 1000_000){
18+
dp[next] = Math.min(dp[i]+1,dp[next]);
19+
}
20+
if ( mul2 <= 1000_000){
21+
dp[mul2] = Math.min(dp[i]+1,dp[mul2]);
22+
}
23+
if ( mul3 <= 1000_000){
24+
dp[mul3] = Math.min(dp[i]+1,dp[mul3]);
25+
}
26+
}
27+
28+
29+
30+
int answer = (dp[y]!=Integer.MAX_VALUE/2?dp[y]:-1);
31+
return answer;
32+
}
33+
}
34+
```

0 commit comments

Comments
 (0)