Skip to content

Commit efee188

Browse files
committed
add 841 script.
1 parent 6240d3e commit efee188

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

841-keys-and-rooms.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[][]} rooms
3+
* @return {boolean}
4+
*/
5+
const canVisitAllRooms = function(rooms) {
6+
const stack = [];
7+
const seen = [];
8+
for (let i = 0; i < rooms.length; i++) {
9+
seen[i] = false;
10+
}
11+
seen[0] = true;
12+
stack.push(0);
13+
while (stack.length) {
14+
let node = stack.pop();
15+
for (let el of rooms[node]) {
16+
if (!seen[el]) {
17+
seen[el] = true;
18+
stack.push(el);
19+
}
20+
}
21+
}
22+
for (let el of seen) {
23+
if (!el) return false;
24+
}
25+
return true;
26+
};

0 commit comments

Comments
 (0)