File tree 2 files changed +29
-0
lines changed
2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change 252
252
821|[ Shortest Distance to a Character] ( ./0821-shortest-distance-to-a-character.js ) |Easy|
253
253
824|[ Goat Latin] ( ./0824-goat-latin.js ) |Easy|
254
254
831|[ Masking Personal Information] ( ./0831-masking-personal-information.js ) |Medium|
255
+ 841|[ Keys and Rooms] ( ./0841-keys-and-rooms.js ) |Medium|
255
256
844|[ Backspace String Compare] ( ./0844-backspace-string-compare.js ) |Easy|
256
257
846|[ Hand of Straights] ( ./0846-hand-of-straights.js ) |Medium|
257
258
867|[ Transpose Matrix] ( ./0867-transpose-matrix.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments