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 <= rooms.length <= 1000
0 <= rooms[i].length <= 1000
- 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:
class Solution {
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
if(rooms == null || rooms.size() == 0){
return true;
} int n = rooms.size();
boolean [] visited = new boolean[n]; LinkedList<Integer> que = new LinkedList<Integer>();
visited[0] = true;
que.add(0);
int res = 0;
while(!que.isEmpty()){
int cur = que.poll();
res++;
for(int key : rooms.get(cur)){
if(!visited[key]){
visited[key] = true;
que.add(key);
}
}
} return res == n;
}
}
Could iterate rooms by DFS too.
Time Complexity: O(V+E).
Space: O(V). stack space.
AC Java:
class Solution {
int res = 0; public boolean canVisitAllRooms(List<List<Integer>> rooms) {
if(rooms == null || rooms.size() == 0){
return true;
} int n = rooms.size();
boolean [] visited = new boolean[n];
dfs(0, rooms, visited); return res == n;
} private void dfs(int cur, List<List<Integer>> rooms, boolean [] visited){
if(visited[cur]){
return;
} visited[cur] = true;
res++; for(int key : rooms.get(cur)){
dfs(key, rooms, visited);
}
}
}
LeetCode 841. Keys and Rooms的更多相关文章
- LC 841. 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, an ...
- 【LeetCode】841. Keys and Rooms 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 841. Keys and Rooms —— weekly contest 86
题目链接:https://leetcode.com/problems/keys-and-rooms/description/ 简单DFS time:9ms 1 class Solution { 2 p ...
- LeetCode 841:钥匙和房间 Keys and Rooms
题目: 有 N 个房间,开始时你位于 0 号房间.每个房间有不同的号码:0,1,2,...,N-1,并且房间里可能有一些钥匙能使你进入下一个房间. 在形式上,对于每个房间 i 都有一个钥匙列表 ...
- [LeetCode] 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, an ...
- LeetCode题解之Keys and Rooms
1.题目描述 2.问题分析 使用深度优先遍历 3.代码 bool canVisitAllRooms(vector<vector<int>>& rooms) { int ...
- [Swift]LeetCode841. 钥匙和房间 | 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, an ...
- [LeetCode] 4 Keys Keyboard 四键的键盘
Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...
- [LeetCode] 2 Keys Keyboard 两键的键盘
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
随机推荐
- Delphi RSA加解密【 (RSA公钥加密,私钥解密)、(RSA私钥加密,公钥解密)、MD5加密、SHA加密】
作者QQ:(648437169) 点击下载➨delphi RSA加解密 [Delphi RSA加解密]支持 (RSA公钥加密,私钥解密).(RSA私钥加密,公钥解密).MD5加密.SHA1加密.SHA ...
- day17——序列化、os、sys、hashlib、collections
day17 序列化 json 两组4个方法: 1.dumps(序列化) -- loads(反序列) dumps(list):将对象转换成字符串 loads(str):将字符串转换成对象 list--s ...
- [开源]Gin + GORM + Casbin+vue-element-admin 实现权限管理系统(golang)
简析 基于 Gin + GORM + Casbin + vue-element-admin 实现的权限管理系统. 基于Casbin 实现RBAC权限管理. 前端实现: vue-element-admi ...
- 不一样的go语言-玩转语法之一
这段时间为俗事所累,疲以应付,落下了不少想法,错过了更新的日子.这个专题开始之际,已经准备了不下十几个主题,而在写作的过程中,又有新想法与主题涌现出来.未来预计想写写的内容主要包括: 玩转语法系列 ...
- pandas.to_datetime() 只保留【年-月-日】
Outline pandas.to_datetime() 生成的日期会默认带有 [2019-07-03 00:00:00]的分钟精度:但有时并不需要这些分钟精度: 去掉分钟精度 可以通过pandas ...
- 自学Python编程的第十天(希望有IT大牛看见的指点小弟我,万分感谢)---------来自苦逼的转行人
2019-09-20-23:24:15 今天逛论坛.逛知识星球时.逛b站up主时,都说到低学历,非科班的人最好不要去自学Python 他们都说:如果我们学python是为了找工作,最好不要把pytho ...
- Windows双系统
基础概念 基础概念 Legacy:传统BIOS传输模式启动顺序:开机→BIOS初始化→BIOS自检→引导操作系统→进入系统.传统硬盘引导记录为MBR格式,MBR无法支持超过2T的硬盘.但拥有最好的兼容 ...
- spring中bean的作用域属性singleton与prototype的区别
1.singleton 当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会 ...
- redux reducer笔记
踩坑一,reducer过于抽象 reducer写得没那么抽象也不会有人怪你的.^_^ reducer其实只有一个,由不同的reducer composition出来的.所以, reducer的父作用域 ...
- SAP Cloud Platform 上CPI的初始化工作
SAP Cloud Platform上的CPI tenant,如果没有正确的初始化,试图使用时会遇到如下错误消息: Insufficient scope for this resourceinsuff ...