-
Notifications
You must be signed in to change notification settings - Fork 112
/
0841-KeysAndRooms.cs
36 lines (32 loc) · 1009 Bytes
/
0841-KeysAndRooms.cs
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
//-----------------------------------------------------------------------------
// Runtime: 108ms
// Memory Usage: 26.5 MB
// Link: https://leetcode.com/submissions/detail/368941673/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0841_KeysAndRooms
{
public bool CanVisitAllRooms(IList<IList<int>> rooms)
{
var seen = new HashSet<int>();
seen.Add(0);
var queue = new Queue<int>();
queue.Enqueue(0);
while (queue.Count > 0)
{
var roomId = queue.Dequeue();
foreach (var key in rooms[roomId])
{
if (!seen.Contains(key))
{
seen.Add(key);
queue.Enqueue(key);
}
}
}
return seen.Count == rooms.Count;
}
}
}