-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisitAllRooms.java
More file actions
48 lines (37 loc) · 1.29 KB
/
Copy pathVisitAllRooms.java
File metadata and controls
48 lines (37 loc) · 1.29 KB
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
41
42
43
44
45
46
47
48
package LeetcodePractice;
import java.util.Arrays;
import java.util.List;
public class VisitAllRooms {
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
boolean[] visited = new boolean[rooms.size()];
dfs(0, rooms, visited);
for (boolean v : visited) {
if (!v) return false;
}
return true;
}
private void dfs(int room, List<List<Integer>> rooms, boolean[] visited) {
if (visited[room]) return;
visited[room] = true;
for (int key : rooms.get(room)) {
dfs(key, rooms, visited);
}
}
public static void main(String[] args) {
VisitAllRooms solution = new VisitAllRooms();
List<List<Integer>> rooms1 = Arrays.asList(
Arrays.asList(1),
Arrays.asList(2),
Arrays.asList(3),
Arrays.asList()
);
List<List<Integer>> rooms2 = Arrays.asList(
Arrays.asList(1, 3),
Arrays.asList(3, 0, 1),
Arrays.asList(2),
Arrays.asList(0)
);
System.out.println("Can visit all rooms (Example 1)? " + solution.canVisitAllRooms(rooms1)); // true
System.out.println("Can visit all rooms (Example 2)? " + solution.canVisitAllRooms(rooms2)); // false
}
}