ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 841. Keys and Rooms

2019-08-04 09:01:18  阅读:427  来源: 互联网

标签:room 841 Keys int rooms key visited true LeetCode


原题链接在这里: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 }

 

标签:room,841,Keys,int,rooms,key,visited,true,LeetCode
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/11297162.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有