We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent c557a2c commit cc32b67Copy full SHA for cc32b67
1 file changed
2025-08-28/김태민/프로그래머스_숫자변환하기.java
@@ -0,0 +1,34 @@
1
+import java.util.*;
2
+class Solution {
3
+ public int solution(int x, int y, int n) {
4
+ int answer = 0;
5
+ return bfs(x,y,n);
6
+ }
7
+
8
+ public int bfs(int x,int y, int n) {
9
+ Queue<int[]> q = new LinkedList<>();
10
+ boolean [] visit = new boolean[y + 1];
11
+ q.offer(new int[] {x, 0});
12
+ while(!q.isEmpty()) {
13
+ int [] a = q.poll();
14
+ if (a[0] == y) {
15
+ return a[1];
16
17
+ for (int i = 0; i < 3;i++) {
18
+ int na = 0;
19
+ if (i == 0) {
20
+ na = a[0] + n;
21
+ } else if (i == 1) {
22
+ na = a[0] * 2;
23
+ } else if (i == 2) {
24
+ na = a[0] * 3;
25
26
+ if (na <= y && !visit[na]) {
27
+ q.offer(new int[] {na,a[1] + 1});
28
+ visit[na] = true;
29
30
31
32
+ return -1;
33
34
+}
0 commit comments