Skip to content

Commit 0a47b12

Browse files
committed
841
1 parent 2ee7477 commit 0a47b12

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

DFS/traditionalDFS/841.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## Keys and Rooms
2+
3+
#### Description
4+
5+
[link](https://leetcode.com/problems/keys-and-rooms/)
6+
7+
---
8+
9+
#### Solution
10+
11+
- See Code
12+
13+
---
14+
15+
#### Code
16+
17+
> 最坏情况:O(n^2)
18+
19+
```python
20+
class Solution:
21+
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
22+
seen = set([0])
23+
N = len(rooms)
24+
state = [0]
25+
while state:
26+
r = state.pop()
27+
for i in rooms[r]:
28+
if i not in seen:
29+
state.append(i)
30+
seen.add(i)
31+
if len(seen) == N: return True
32+
return False
33+
```

0 commit comments

Comments
 (0)