Skip to content

Instantly share code, notes, and snippets.

@fengyitsai
Created January 15, 2020 13:07
Show Gist options
  • Save fengyitsai/de6c4047c66ff6dd2944cdf6242ce1f1 to your computer and use it in GitHub Desktop.
Save fengyitsai/de6c4047c66ff6dd2944cdf6242ce1f1 to your computer and use it in GitHub Desktop.
841. Keys and Rooms
class Solution {
func canVisitAllRooms(_ rooms: [[Int]]) -> Bool {
var visited = Set<Int>()
var queue = [0]
while !queue.isEmpty {
var nextQueue = [Int]()
for room in queue where !visited.contains(room) {
nextQueue.append(contentsOf: rooms[room])
visited.insert(room)
}
queue = nextQueue
}
return visited.count == rooms.count
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment