-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path752-Open-the-Lock.js
45 lines (36 loc) · 1.24 KB
/
752-Open-the-Lock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* @param {string[]} deadends
* @param {string} target
* @return {number}
*/
const openLock = (deadends, target) => {
const blocked = new Set(deadends);
const visited = new Set();
if (blocked.has('0000')) return -1;
visited.add('0000');
let queue = ['0000'];
let distance = 0;
while (queue.length) {
const nextQueue = [];
for (const combination of queue) {
if (combination === target) return distance;
for (let i = 0; i < combination.length; i++) {
const up = (+combination[i] + 1) % 10;
const down = (+combination[i] + 9) % 10;
const nextUp = combination.slice(0, i) + up + combination.slice(i + 1);
const nextDown = combination.slice(0, i) + down + combination.slice(i + 1);
if (!visited.has(nextUp) && !blocked.has(nextUp)) {
nextQueue.push(nextUp);
visited.add(nextUp);
}
if (!visited.has(nextDown) && !blocked.has(nextDown)) {
nextQueue.push(nextDown);
visited.add(nextDown);
}
}
}
distance++;
queue = nextQueue;
}
return -1;
};