-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsolution.ts
78 lines (56 loc) · 1.76 KB
/
solution.ts
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* @lc app=leetcode id=752 lang=javascript
*
* [752] Open the Lock
*/
// @lc code=start
/**
* @param {string[]} deadends
* @param {string} target
* @return {number}
*/
const openLock = (deadends: string[], target: string): number => {
// * inspired by https://leetcode-cn.com/problems/open-the-lock/solution/javascriptti-jie-bfs-es6-set-220-ms-by-user8191/
// * this's my fastest ['124 ms', '94.29 %', '42.3 MB', '100 %']
if (deadends.includes('0000') || deadends.includes(target)) return -1;
if (target === '0000') return 0;
// * ---------------- prepare
type Code = number;
// * use number, not string, without data type transform, it's a litte faster
const numTarget = Number(target);
const steps: Record<Code, number> = { 0: 0 };
deadends.forEach((code) => {
steps[Number(code)] = -1;
});
// * ---------------- bfsWalker
const bfsWalker = (cur: Code): Code[] => {
const neighbors: Code[] = [];
// * this version's calculation is a bit complex, it's using number
for (let i = 0; i < 4; i++) {
const base = 10 ** i;
const c = ~~(cur / base) % 10;
neighbors.push(c === 9 ? cur - 9 * base : cur + 1 * base);
neighbors.push(c === 0 ? cur + 9 * base : cur - 1 * base);
}
return neighbors.filter((e) => steps[e] === undefined);
};
// * ---------------- start searching
const queue: Code[] = [0];
let cur: Code;
while (queue.length) {
cur = queue.shift()!;
const nextStep = steps[cur] + 1;
if (cur === numTarget) {
steps[numTarget] = nextStep - 1;
break;
}
const neighbors = bfsWalker(cur);
neighbors.forEach((e) => {
steps[e] = nextStep;
queue.push(e);
});
}
return steps[numTarget] ?? -1;
};
// @lc code=end
export { openLock };