-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution.ts
40 lines (32 loc) · 809 Bytes
/
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
/*
* @lc app=leetcode id=841 lang=javascript
*
* [841] Keys and Rooms
*/
// @lc code=start
/**
* @param {number[][]} rooms
* @return {boolean}
*/
const canVisitAllRooms = (rooms: number[][]): boolean => {
// * ['56 ms', '86.69 %', '35.4 MB', '100 %']
const keyPool = new Set<number>();
keyPool.add(0);
const visited = new Set<number>();
const checkList: number[] = [0];
while (checkList.length) {
const roomNumber = checkList.pop()!;
if (keyPool.size === rooms.length) break;
if (!visited.has(roomNumber)) {
visited.add(roomNumber);
const keys = rooms[roomNumber];
keys.forEach((key) => {
keyPool.add(key);
checkList.push(key);
});
}
}
return keyPool.size === rooms.length;
};
// @lc code=end
export { canVisitAllRooms };