forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_841.java
61 lines (58 loc) · 2.05 KB
/
_841.java
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
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.fishercoder.solutions;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import java.util.TreeSet;
public class _841 {
public static class Solution1 {
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
Set<Integer> unvisitedRooms = new HashSet<>();
for (int i = 1; i < rooms.size(); i++) {
unvisitedRooms.add(i);
}
List<Integer> keys = rooms.get(0);
Queue<Integer> queue = new LinkedList<>();
for (int key : keys) {
queue.offer(key);
}
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
int roomIndex = queue.poll();
unvisitedRooms.remove(roomIndex);
for (int j = 0; j < rooms.get(roomIndex).size(); j++) {
Integer nextRoom = rooms.get(roomIndex).get(j);
if (unvisitedRooms.contains(nextRoom) && !queue.contains(nextRoom)) {
queue.offer(nextRoom);
}
}
}
}
return unvisitedRooms.isEmpty();
}
}
public static class Solution2 {
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
TreeSet<Integer> treeSet = new TreeSet<>();
Set<Integer> visited = new HashSet<>();
visited.add(0);
treeSet.addAll(rooms.get(0));
while (!treeSet.isEmpty()) {
Integer key = treeSet.pollFirst();
if (!visited.add(key)) {
continue;
}
if (visited.size() == rooms.size()) {
return true;
}
treeSet.addAll(rooms.get(key));
}
if (visited.size() == rooms.size()) {
return true;
}
return false;
}
}
}