Skip to content

Commit 62f2010

Browse files
committed
Leetcode 841 Keys and Rooms
1 parent 3f7289d commit 62f2010

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed
File renamed without changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
3+
boolean[] visited = new boolean[rooms.size()];
4+
visited[0] = true;
5+
DFS(rooms, 0, visited);
6+
7+
for(boolean v : visited)
8+
if(!v) return false;
9+
10+
return true;
11+
}
12+
13+
private void DFS(List<List<Integer>> rooms, int room, boolean[] visited) {
14+
List<Integer> keys = rooms.get(room);
15+
for(int key : keys) {
16+
if(visited[key]) continue;
17+
visited[key] = true;
18+
DFS(rooms, key, visited);
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)