Java/Java-1차캐시

[프로그래머스] 숫자 변환하기

프로버티기 2025. 4. 12. 13:37

문제

https://school.programmers.co.kr/learn/courses/30/lessons/154538

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

 


 

코드 

 


import java.util.*;

class Solution {
    public int solution(int x, int y, int n) {
        Queue<int[]> queue = new LinkedList<>();
        boolean[] visited = new boolean[y + 1]; // 0~y 까지

        queue.add(new int[]{y, 0});
        visited[y] = true;

        while (!queue.isEmpty()) {
            int[] current = queue.poll();
            int value = current[0];
            int count = current[1];

            if (value == x) return count;

            if (value % 2 == 0 && !visited[value / 2]) {
                visited[value / 2] = true;
                queue.add(new int[]{value / 2, count + 1});
            }

            if (value % 3 == 0 && !visited[value / 3]) {
                visited[value / 3] = true;
                queue.add(new int[]{value / 3, count + 1});
            }

            if (value - n >= x && !visited[value - n]) {
                visited[value - n] = true;
                queue.add(new int[]{value - n, count + 1});
            }
        }

        return -1;
    }
}