Skip to content

Commit f0b143b

Browse files
committedJan 13, 2025
Add solution #841
1 parent 9924d5f commit f0b143b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
821|[Shortest Distance to a Character](./0821-shortest-distance-to-a-character.js)|Easy|
253253
824|[Goat Latin](./0824-goat-latin.js)|Easy|
254254
831|[Masking Personal Information](./0831-masking-personal-information.js)|Medium|
255+
841|[Keys and Rooms](./0841-keys-and-rooms.js)|Medium|
255256
844|[Backspace String Compare](./0844-backspace-string-compare.js)|Easy|
256257
846|[Hand of Straights](./0846-hand-of-straights.js)|Medium|
257258
867|[Transpose Matrix](./0867-transpose-matrix.js)|Easy|

‎solutions/0841-keys-and-rooms.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 841. Keys and Rooms
3+
* https://leetcode.com/problems/keys-and-rooms/
4+
* Difficulty: Medium
5+
*
6+
* There are n rooms labeled from 0 to n - 1 and all the rooms are locked except for room 0.
7+
* Your goal is to visit all the rooms. However, you cannot enter a locked room without
8+
* having its key.
9+
*
10+
* When you visit a room, you may find a set of distinct keys in it. Each key has a number
11+
* on it, denoting which room it unlocks, and you can take all of them with you to unlock
12+
* the other rooms.
13+
*
14+
* Given an array rooms where rooms[i] is the set of keys that you can obtain if you visited
15+
* room i, return true if you can visit all the rooms, or false otherwise.
16+
*/
17+
18+
/**
19+
* @param {number[][]} rooms
20+
* @return {boolean}
21+
*/
22+
var canVisitAllRooms = function(rooms) {
23+
const set = new Set([0]);
24+
for (const key of set) {
25+
rooms[key].forEach(value => set.add(value));
26+
}
27+
return set.size === rooms.length;
28+
};

0 commit comments

Comments
 (0)
Please sign in to comment.