博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 841. Keys and Rooms
阅读量:4622 次
发布时间:2019-06-09

本文共 2916 字,大约阅读时间需要 9 分钟。

原题链接在这里:

题目:

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: trueExplanation:  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: falseExplanation: 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
> 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
que = new LinkedList
();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
> 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
> 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 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11297162.html

你可能感兴趣的文章
spi驱动无法建立spidev问题
查看>>
ANDROID开发之SQLite详解
查看>>
如何依靠代码提高网络性能
查看>>
Fedora 17 无线网卡配置笔记
查看>>
[HNOI 2001]求正整数
查看>>
plsql出现录相机后卡屏解决方法
查看>>
HDU 1004 Let the Balloon Rise
查看>>
Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses
查看>>
numpy.random.shuffle()与numpy.random.permutation()的区别
查看>>
Zookeeper要安装在奇数个节点,但是为什么?
查看>>
discuz 微社区安装记录
查看>>
[BZOJ4824][Cqoi2017]老C的键盘 树形dp+组合数
查看>>
配置的热更新
查看>>
MySQL事务的开启与提交,autocommit自动提交功能
查看>>
PriorityQueue
查看>>
CODEVS1403 新三国争霸
查看>>
iOS 环信离线推送
查看>>
WPFTookit Chart 高级进阶
查看>>
thulac安装问题
查看>>
你必须知道的.NET Day1
查看>>