Skip to content

Commit 4833179

Browse files
authored
[20251109] PGM / LV2 / 숫자 변환하기 / 이인희
1 parent f6eb215 commit 4833179

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
```java
2+
import java.util.*;
3+
4+
class Solution {
5+
private static int MAX = 1_000_001;
6+
public int solution(int x, int y, int n) {
7+
int answer = 0;
8+
Queue<int[]> q = new ArrayDeque<int[]>();
9+
q.offer(new int[]{0, x});
10+
boolean[] visited = new boolean[MAX];
11+
visited[x] = true;
12+
boolean hasAnswer = false;
13+
int count = 0, value;
14+
while(!q.isEmpty()){
15+
int[] temp = q.poll();
16+
count = temp[0];
17+
value = temp[1];
18+
if(value == y){
19+
hasAnswer = true;
20+
break;
21+
}
22+
for(int new_value: new int[]{value+n, value*2, value*3}){
23+
if(new_value >= MAX){
24+
continue;
25+
}
26+
if(visited[new_value])
27+
continue;
28+
q.offer(new int[]{count+1, new_value});
29+
visited[new_value] = true;
30+
}
31+
32+
}
33+
return (hasAnswer)? count: -1;
34+
}
35+
}
36+
```

0 commit comments

Comments
 (0)