LeetCode 841. Keys and Rooms

原题链接在这里:https://leetcode.com/problems/keys-and-rooms/

题目:

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. 1 <= rooms.length <= 1000
  2. 0 <= rooms[i].length <= 1000
  3. The number of keys in all rooms combined is at most 3000.

题解:

Could traverse rooms by BFS.

Get key lists, if not visited before, add it into queue.

Check visited rooms count == N.

Time Complexity: O(V+E). V = total number of rooms. E = total number of keys.

Space: O(V).

AC Java:

 1 class Solution {
 2     public boolean canVisitAllRooms(List<List<Integer>> rooms) {
 3         if(rooms == null || rooms.size() == 0){
 4             return true;
 5         }
 6         
 7         int n = rooms.size();
 8         boolean [] visited = new boolean[n];
 9         
10         LinkedList<Integer> que = new LinkedList<Integer>();
11         visited[0] = true;
12         que.add(0);
13         int res = 0;
14         while(!que.isEmpty()){
15             int cur = que.poll();
16             res++;
17             for(int key : rooms.get(cur)){
18                 if(!visited[key]){
19                     visited[key] = true;
20                     que.add(key);
21                 }
22             }
23         }
24         
25         return res == n;
26     }
27 }

Could iterate rooms by DFS too.

Time Complexity: O(V+E).

Space: O(V). stack space.

AC Java:

 1 class Solution {
 2     int res = 0;
 3     
 4     public boolean canVisitAllRooms(List<List<Integer>> rooms) {
 5         if(rooms == null || rooms.size() == 0){
 6             return true;
 7         }
 8         
 9         int n = rooms.size();
10         boolean [] visited = new boolean[n];
11         dfs(0, rooms, visited);
12         
13         return res == n;
14     }
15     
16     private void dfs(int cur, List<List<Integer>> rooms, boolean [] visited){
17         if(visited[cur]){
18             return;
19         }
20         
21         visited[cur] = true;
22         res++;
23         
24         for(int key : rooms.get(cur)){
25             dfs(key, rooms, visited);
26         }
27     }
28 }

猜你喜欢

转载自www.cnblogs.com/Dylan-Java-NYC/p/11297162.html
今日推荐