diff --git "a/programmers/Lv.2/\354\210\253\354\236\220 \353\263\200\355\231\230\355\225\230\352\270\260/main.js" "b/programmers/Lv.2/\354\210\253\354\236\220 \353\263\200\355\231\230\355\225\230\352\270\260/main.js" new file mode 100644 index 0000000..db61668 --- /dev/null +++ "b/programmers/Lv.2/\354\210\253\354\236\220 \353\263\200\355\231\230\355\225\230\352\270\260/main.js" @@ -0,0 +1,20 @@ +function solution(x, y, n) { + const q = []; + q.push([y, 0]); + while (q.length > 0) { + let cur = q.shift(); + if (cur[0] === x) { + return cur[1]; + } + if (cur[0] - n > x && (cur[0] - n) % 1 === 0) { + q.push([cur[0] - n, cur[1] + 1]); + } else if (cur[0] - n === x) return cur[1] + 1; + if (cur[0] % 2 === 0 && cur[0] / 2 > x) { + q.push([cur[0] / 2, cur[1] + 1]); + } else if (cur[0] / 2 === x) return cur[1] + 1; + if (cur[0] % 3 === 0 && cur[0] / 3 > x) { + q.push([cur[0] / 3, cur[1] + 1]); + } else if (cur[0] / 3 === x) return cur[1] + 1; + } + return -1; +}