Skip to content

Commit 692e700

Browse files
committed
add JS solution for problem 752
1 parent 4a57286 commit 692e700

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ If you would like to have collaborator permissions on the repo to merge your own
332332
<sub>[0994 - Rotting Oranges](https://leetcode.com/problems/rotting-oranges/)</sub> | <sub><div align='center'>[✔️](c%2F0994-rotting-oranges.c)</div></sub> | <sub><div align='center'>[✔️](cpp%2F0994-rotting-oranges.cpp)</div></sub> | <sub><div align='center'>[✔️](csharp%2F0994-rotting-oranges.cs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](go%2F0994-rotting-oranges.go)</div></sub> | <sub><div align='center'>[✔️](java%2F0994-rotting-oranges.java)</div></sub> | <sub><div align='center'>[✔️](javascript%2F0994-rotting-oranges.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F0994-rotting-oranges.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0994-rotting-oranges.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](typescript%2F0994-rotting-oranges.ts)</div></sub>
333333
<sub>[0286 - Walls And Gates](https://leetcode.com/problems/walls-and-gates/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](cpp%2F0286-walls-and-gates.cpp)</div></sub> | <sub><div align='center'>[✔️](csharp%2F0286-walls-and-gates.cs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](java%2F0286-walls-and-gates.java)</div></sub> | <sub><div align='center'>[✔️](javascript%2F0286-walls-and-gates.js)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](python%2F0286-walls-and-gates.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub>
334334
<sub>[0909 - Snakes And Ladders](https://leetcode.com/problems/snakes-and-ladders/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](csharp%2F0909-snakes-and-ladders.cs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](java%2F0909-snakes-and-ladders.java)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](python%2F0909-snakes-and-ladders.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub>
335-
<sub>[0752 - Open The Lock](https://leetcode.com/problems/open-the-lock/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](csharp%2F0752-open-the-lock.cs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](java%2F0752-open-the-lock.java)</div></sub> | <sub><div align='center'></div></sub> | <sub><div align='center'>[✔️](kotlin%2F0752-open-the-lock.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0752-open-the-lock.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub>
335+
<sub>[0752 - Open The Lock](https://leetcode.com/problems/open-the-lock/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](csharp%2F0752-open-the-lock.cs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](java%2F0752-open-the-lock.java)</div></sub> | <sub><div align='center'>[✔️](javascript%2F0752-open-the-lock.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F0752-open-the-lock.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0752-open-the-lock.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub>
336336
<sub>[0802 - Find Eventual Safe States](https://leetcode.com/problems/find-eventual-safe-states/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](java%2F0802-find-eventual-safe-states.java)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](kotlin%2F0802-find-eventual-safe-states.kt)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub>
337337
<sub>[0207 - Course Schedule](https://leetcode.com/problems/course-schedule/)</sub> | <sub><div align='center'>[✔️](c%2F0207-course-schedule.c)</div></sub> | <sub><div align='center'>[✔️](cpp%2F0207-course-schedule.cpp)</div></sub> | <sub><div align='center'>[✔️](csharp%2F0207-course-schedule.cs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](go%2F0207-course-schedule.go)</div></sub> | <sub><div align='center'>[✔️](java%2F0207-course-schedule.java)</div></sub> | <sub><div align='center'>[✔️](javascript%2F0207-course-schedule.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F0207-course-schedule.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0207-course-schedule.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](swift%2F0207-course-schedule.swift)</div></sub> | <sub><div align='center'>[✔️](typescript%2F0207-course-schedule.ts)</div></sub>
338338
<sub>[0210 - Course Schedule II](https://leetcode.com/problems/course-schedule-ii/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](cpp%2F0210-course-schedule-ii.cpp)</div></sub> | <sub><div align='center'>[✔️](csharp%2F0210-course-schedule-ii.cs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](go%2F0210-course-schedule-ii.go)</div></sub> | <sub><div align='center'>[✔️](java%2F0210-course-schedule-ii.java)</div></sub> | <sub><div align='center'>[✔️](javascript%2F0210-course-schedule-ii.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F0210-course-schedule-ii.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0210-course-schedule-ii.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](typescript%2F0210-course-schedule-ii.ts)</div></sub>

javascript/0752-open-the-lock.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @param {string[]} deadends
3+
* @param {string} target
4+
* @return {number}
5+
*/
6+
var openLock = function(deadends, target) {
7+
8+
// General approach:
9+
// Start at the end, mark that spot to be 0 away from target
10+
// Find all the valid neighbors through bfs, mark those as 1
11+
// Find all _their_ valid neighbors, mark those as ++ etc
12+
// Until we find 0000. Whatever we mark that as is the number. BFS will guarantee it's the shortest path
13+
14+
let q = [target]; // our BFS Queue
15+
let mem = {}; // to keep track what we already have visited
16+
mem[target] = 0; // starting distance of the end
17+
18+
// Helper function that given a position, will generate all the numbers we
19+
// can create in 1 move;
20+
let getNextPositions = function (pos) {
21+
// one above, one below
22+
let dir = [-1, 1];
23+
let arr = pos.split('');
24+
let positions = [];
25+
let i, j;
26+
27+
for (j = 0; j < 2; j++) {
28+
let next = '';
29+
// for each number in the position
30+
for (i = 0; i < 4; i++) {
31+
// logic is not just +1 -1, have to deal with wrapping around
32+
let n = (10 + parseInt(arr[i], 10) + dir[j]) % 10;
33+
// clone to not ruin our array for the next number
34+
let next = [...arr];
35+
// set our 1 change
36+
next[i] = n;
37+
positions.push(next.join(''));
38+
}
39+
}
40+
return positions;
41+
}
42+
43+
while (q.length) {
44+
// dequeue a position to check out
45+
let pos = q.shift();
46+
47+
// if it's 0000 we're done. BFS guarantees it's the shortest possible
48+
if (pos === '0000') {
49+
return mem[pos];
50+
} else {
51+
let next = getNextPositions(pos);
52+
next.forEach(function(n) {
53+
// if we haven't seen n before, and it's not a dead end,
54+
if (mem[n] === undefined && !deadends.includes(n)) {
55+
// mark the distance and enqueue to check out next
56+
mem[n] = mem[pos] + 1;
57+
q.push(n);
58+
}
59+
})
60+
}
61+
}
62+
// if we end up here, we couldn't find it
63+
return -1;
64+
};

0 commit comments

Comments
 (0)