leetcode841 Keys and Rooms

 1 """
 2 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.
 3 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.
 4 Initially, all the rooms start locked (except for room 0).
 5 You can walk back and forth between rooms freely.
 6 Return true if and only if you can enter every room.
 7 Example 1:
 8 Input: [[1],[2],[3],[]]
 9 Output: true
10 Explanation:
11 We start in room 0, and pick up key 1.
12 We then go to room 1, and pick up key 2.
13 We then go to room 2, and pick up key 3.
14 We then go to room 3.  Since we were able to go to every room, we return true.
15 Example 2:
16 Input: [[1,3],[3,0,1],[2],[0]]
17 Output: false
18 Explanation: We can‘t enter the room with number 2.
19 """
20 """
21 有两种方法,分别是BFS和DFS
22 解法一:BFS
23 用一个queue来存能到达的room
24 用一个set来存能拿到的房间钥匙
25 返回值为set里的钥匙数 和 房间数是否相等
26 """
27 class Solution1:
28     def canVisitAllRooms(self, rooms):
29         queue = [0]
30         s = set()
31         s.add(0)  #!!!bug 没有初始化开0门的钥匙
32         while queue:
33             keys = queue.pop()
34             for key in rooms[keys]:
35                 if key not in s:
36                     s.add(key)
37                     queue.append(key)
38         return len(s) == len(rooms)
39
40 """
41 解法二:DFS
42 建立一个set存钥匙,从rooms[0]开始递归
43 将找到的key存入set里
44 继续递归访问rooms[key]
45 """
46 class Solution2:
47     def canVisitAllRooms(self, rooms):
48         s = set()
49         s.add(0)
50         def enterroom(keys):
51             for key in keys:
52                 if key not in s:
53                     s.add(key)
54                     enterroom(rooms[key])
55                 # else:
56                 #     pass
57             return
58         enterroom(rooms[0])
59         return len(s) == len(rooms)

原文地址:https://www.cnblogs.com/yawenw/p/12314985.html

时间: 2024-11-06 07:42:48

leetcode841 Keys and Rooms的相关文章

LeetCode 841:钥匙和房间 Keys and Rooms

题目: ? 有 N 个房间,开始时你位于 0 号房间.每个房间有不同的号码:0,1,2,...,N-1,并且房间里可能有一些钥匙能使你进入下一个房间. ? 在形式上,对于每个房间 i 都有一个钥匙列表 rooms[i],每个钥匙 rooms[i][j] 由 [0,1,...,N-1] 中的一个整数表示,其中 N = rooms.length. 钥匙 rooms[i][j] = v 可以打开编号为 v 的房间. 最初,除 0 号房间外的其余所有房间都被锁住. 你可以自由地在房间之间来回走动. 如果

841. Keys and Rooms —— weekly contest 86

题目链接:https://leetcode.com/problems/keys-and-rooms/description/ 简单DFS time:9ms 1 class Solution { 2 public: 3 void DFS(int root,vector<int>& visited,vector<vector<int>>& rooms){ 4 visited[root] = 1; 5 for(auto x : rooms[root]){ 6

Socket.IO API Socket

Socket Socket类继承自EventEmitter.覆写了emit方法,保留了EventEmitter的其他方法 socket.id 会话的唯一标识符,来自于底层的Client socket.rooms 标识此客户端所在房间的字符串哈希值,按房间名称编制索引 socket.client 对底层Client对象的引用. socket.conn 对底层Client传输连接的引用(engine.io Socket对象) socket.request 一个getter代理,它返回对request

【LeetCode】深搜DFS(共85题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [104]Maximum Depth of Binary Tree [105]Construct Binary Tree from Preorder and Inorder

HDU 3625 Examining the Rooms

Problem Description A murder happened in the hotel. As the best detective in the town, you should examine all the N rooms of the hotel immediately. However, all the doors of the rooms are locked, and the keys are just locked in the rooms, what a trap

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

[PostgreSQL] Use Foreign Keys to Ensure Data Integrity in Postgres

Every movie needs a director and every rented movie needs to exist in the store. How do we make sure something in another table exists before inserting new data? This lesson will teach us about foreign keys and references. CREATE TABLE directors ( id

Redis学习笔记之五:redis keys命令 (24个)

Redis 版本:3.2.100 Redis 命令 Redis 命令用于在 redis 服务上执行操作. 要在 redis 服务上执行命令需要一个 redis 客户端. 一.客户端连接服务器 基本语法: redis-cli [-h host -p port -a password] 不带参数,则默认连接本地,带上三个参数,则连接远程服务器. 举例: 如何连接到主机为 127.0.0.1,端口为 6379 ,密码为 mypass 的 redis 服务上. redis-cli -h 127.0.0.

2、keys相关命令

redis的官网http://redis.io是学习redis的重要资源库,所有命令都分门别类的罗列在了这里http://redis.io/commands. 1.数据库选择命令: SELECT index //选择当前连接使用哪个数据库 默认配置下一个redis-server服务会开启16个数据库,其index位0~15,可以通过SELECT命令来选择使用哪个库,redis-cli连接默认使用0号库. 注意:redis命令都是作用在已选择的库上面.比如,你使用“SELECT 1”选择了1号库,