Skip to content

Commit

Permalink
80차 1번 문제풀이이
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-hjh committed Feb 14, 2025
1 parent 2cc69a2 commit b138a6c
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions live7/test80/문제1/황장현.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const input = require('fs')
.readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt')
.toString()
.trim()
.split('\n')
.map((el) => el.split(' ').map(Number));

function solution(input) {
const [N, K] = input[0];

const visited = Array(100001).fill(false);

if (N >= K) {
return N - K;
}

const queue = [[N, 0]];
visited[N] = true;

while (queue.length) {
const [X, time] = queue.shift();

if (X === K) return time;

if (X * 2 <= 100000 && !visited[X * 2]) {
visited[X * 2] = true;
queue.push([X * 2, time]);
}
if (X - 1 >= 0 && !visited[X - 1]) {
visited[X - 1] = true;
queue.push([X - 1, time + 1]);
}
if (X + 1 <= 100000 && !visited[X + 1]) {
visited[X + 1] = true;
queue.push([X + 1, time + 1]);
}
}
}

console.log(solution(input));

0 comments on commit b138a6c

Please sign in to comment.