Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/841.html
841. Keys and Rooms (Medium)
There are N
rooms and you start in room 0
. Each room has a distinct number in 0, 1, 2, ..., N-1
, and each room may have some keys to access the next room.
Formally, each room i
has a list of keys rooms[i]
, and each key rooms[i][j]
is an integer in [0, 1, ..., N-1]
where N = rooms.length
. A key rooms[i][j] = v
opens the room with number v
.
Initially, all the rooms start locked (except for room 0
).
You can walk back and forth between rooms freely.
Return true
if and only if you can enter every room.
Example 1:
Input: [[1],[2],[3],[]] Output: true Explanation: We start in room 0, and pick up key 1. We then go to room 1, and pick up key 2. We then go to room 2, and pick up key 3. We then go to room 3. Since we were able to go to every room, we return true.
Example 2:
Input: [[1,3],[3,0,1],[2],[0]] Output: false Explanation: We can't enter the room with number 2.
Note:
1 <= rooms.length <= 1000
0 <= rooms[i].length <= 1000
- The number of keys in all rooms combined is at most
3000
.
Companies:
Google
Related Topics:
Depth-first Search, Graph
Solution 1.
-
class Solution { public boolean canVisitAllRooms(List<List<Integer>> rooms) { int length = rooms.size(); boolean[] visited = new boolean[length]; int visitCount = 0; Queue<Integer> queue = new LinkedList<Integer>(); queue.offer(0); while (!queue.isEmpty()) { int room = queue.poll(); if (!visited[room]) { visited[room] = true; visitCount++; List<Integer> keys = rooms.get(room); for (int key : keys) { if (!visited[key]) queue.offer(key); } } } return visitCount == length; } } ############ class Solution { private List<List<Integer>> rooms; private Set<Integer> vis; public boolean canVisitAllRooms(List<List<Integer>> rooms) { vis = new HashSet<>(); this.rooms = rooms; dfs(0); return vis.size() == rooms.size(); } private void dfs(int u) { if (vis.contains(u)) { return; } vis.add(u); for (int v : rooms.get(u)) { dfs(v); } } }
-
// OJ: https://leetcode.com/problems/keys-and-rooms/ // Time: O(K) where K is the count of all the keys. // Space: O(N) where N is the count of rooms class Solution { public: bool canVisitAllRooms(vector<vector<int>>& A) { int N = A.size(), cnt = 0; vector<bool> seen(N); seen[0] = true; queue<int> q; q.push(0); while (q.size()) { int i = q.front(); q.pop(); ++cnt; for (int j : A[i]) { if (seen[j]) continue; seen[j] = true; q.push(j); } } return cnt == N; } };
-
class Solution: def canVisitAllRooms(self, rooms: List[List[int]]) -> bool: def dfs(u): if u in vis: return vis.add(u) for v in rooms[u]: dfs(v) vis = set() dfs(0) return len(vis) == len(rooms) ############ class Solution: def canVisitAllRooms(self, rooms): """ :type rooms: List[List[int]] :rtype: bool """ visited = [0] * len(rooms) self.dfs(rooms, 0, visited) return sum(visited) == len(rooms) def dfs(self, rooms, index, visited): visited[index] = 1 for key in rooms[index]: if not visited[key]: self.dfs(rooms, key, visited)
-
func canVisitAllRooms(rooms [][]int) bool { vis := make(map[int]bool) var dfs func(u int) dfs = func(u int) { if vis[u] { return } vis[u] = true for _, v := range rooms[u] { dfs(v) } } dfs(0) return len(vis) == len(rooms) }
-
function canVisitAllRooms(rooms: number[][]): boolean { const n = rooms.length; const isOpen = new Array(n).fill(false); const keys = [0]; while (keys.length !== 0) { const i = keys.pop(); if (isOpen[i]) { continue; } isOpen[i] = true; keys.push(...rooms[i]); } return isOpen.every(v => v); }
-
impl Solution { pub fn can_visit_all_rooms(rooms: Vec<Vec<i32>>) -> bool { let n = rooms.len(); let mut is_open = vec![false; n]; let mut keys = vec![0]; while !keys.is_empty() { let i = keys.pop().unwrap(); if is_open[i] { continue; } is_open[i] = true; rooms[i].iter().for_each(|&key| keys.push(key as usize)); } is_open.iter().all(|&v| v) } }