Skip to content

Commit 597a926

Browse files
authored
feat: add ts solution to lc problem: No.0752 (#2995)
1 parent 4c2d2fc commit 597a926

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

solution/0700-0799/0752.Open the Lock/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,39 @@ func openLock(deadends []string, target string) int {
267267
}
268268
```
269269

270+
#### TypeScript
271+
272+
```ts
273+
function openLock(deadends: string[], target: string): number {
274+
const vis = Array<boolean>(10_000);
275+
const q = [[0, 0, 0, 0, 0]];
276+
const t = +target;
277+
deadends.forEach(s => (vis[+s] = true));
278+
279+
for (const [a, b, c, d, ans] of q) {
280+
const key = a * 1000 + b * 100 + c * 10 + d;
281+
if (vis[key]) {
282+
continue;
283+
}
284+
285+
vis[key] = true;
286+
if (key === t) {
287+
return ans;
288+
}
289+
290+
for (let i = 0; i < 4; i++) {
291+
const next = [a, b, c, d, ans + 1];
292+
const prev = [a, b, c, d, ans + 1];
293+
next[i] = (next[i] + 11) % 10;
294+
prev[i] = (prev[i] + 9) % 10;
295+
q.push(prev, next);
296+
}
297+
}
298+
299+
return -1;
300+
}
301+
```
302+
270303
<!-- tabs:end -->
271304

272305
<!-- solution:end -->

solution/0700-0799/0752.Open the Lock/README_EN.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,39 @@ func openLock(deadends []string, target string) int {
263263
}
264264
```
265265

266+
#### TypeScript
267+
268+
```ts
269+
function openLock(deadends: string[], target: string): number {
270+
const vis = Array<boolean>(10_000);
271+
const q = [[0, 0, 0, 0, 0]];
272+
const t = +target;
273+
deadends.forEach(s => (vis[+s] = true));
274+
275+
for (const [a, b, c, d, ans] of q) {
276+
const key = a * 1000 + b * 100 + c * 10 + d;
277+
if (vis[key]) {
278+
continue;
279+
}
280+
281+
vis[key] = true;
282+
if (key === t) {
283+
return ans;
284+
}
285+
286+
for (let i = 0; i < 4; i++) {
287+
const next = [a, b, c, d, ans + 1];
288+
const prev = [a, b, c, d, ans + 1];
289+
next[i] = (next[i] + 11) % 10;
290+
prev[i] = (prev[i] + 9) % 10;
291+
q.push(prev, next);
292+
}
293+
}
294+
295+
return -1;
296+
}
297+
```
298+
266299
<!-- tabs:end -->
267300

268301
<!-- solution:end -->
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function openLock(deadends: string[], target: string): number {
2+
const vis = Array<boolean>(10_000);
3+
const q = [[0, 0, 0, 0, 0]];
4+
const t = +target;
5+
deadends.forEach(s => (vis[+s] = true));
6+
7+
for (const [a, b, c, d, ans] of q) {
8+
const key = a * 1000 + b * 100 + c * 10 + d;
9+
if (vis[key]) {
10+
continue;
11+
}
12+
13+
vis[key] = true;
14+
if (key === t) {
15+
return ans;
16+
}
17+
18+
for (let i = 0; i < 4; i++) {
19+
const next = [a, b, c, d, ans + 1];
20+
const prev = [a, b, c, d, ans + 1];
21+
next[i] = (next[i] + 11) % 10;
22+
prev[i] = (prev[i] + 9) % 10;
23+
q.push(prev, next);
24+
}
25+
}
26+
27+
return -1;
28+
}

0 commit comments

Comments
 (0)