package
0.0.0-20241220224003-b7cf03a90b2b
Repository: https://github.com/szhou12/leetcode-go.git
Documentation: pkg.go.dev
# README
841. Keys and Rooms
Solution idea
DFS
- 题目要求只要能从
room 0
出发到达所有其他rooms就行- 不强求从
room 0
出发,必须遍历所有rooms,最后到达room n-1
- 不强求从
- input graph可能有环,所以用 hashmap
visited
记录遍历过的节点以避免回到遍历过的节点- 同时,
visited
记录了从room 0
可以遍历到的所以rooms, 最后只用检查哪个room
不在visited
中就可以决定返回true/false
- 同时,
Time complexity = $O(V+E)$