Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ba88122

Browse files
rain84yanglbme
andauthored
feat: add solutions to lc problem: No.841 (#2887)
* feat: add iterative TS solution to lc problem: No.841 * Update README.md * Update README_EN.md * Update Solution2.ts * Create Solution2.py * Create Solution2.java * Create Solution2.cpp * Create Solution2.go * style: format code and docs with prettier --------- Co-authored-by: Libin YANG <contact@yanglibin.info>
1 parent 468d402 commit ba88122

File tree

7 files changed

+350
-0
lines changed

7 files changed

+350
-0
lines changed

‎solution/0800-0899/0841.Keys and Rooms/README.md‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,135 @@ impl Solution {
218218

219219
<!-- solution:end -->
220220

221+
<!-- solution:start -->
222+
223+
### 方法二:BFS
224+
225+
我们也可以使用广度优先搜索的方法遍历整张图,用一个哈希表或者数组 `vis` 标记当前节点是否访问过,以防止重复访问。
226+
227+
具体地,我们定义一个队列 $q,ドル初始时将节点 0ドル$ 放入队列中,然后不断遍历队列。每次取出队首节点 $i,ドル如果 $i$ 被访问过则直接跳过,否则我们将其标记为已访问,然后将 $i$ 可以到达的节点加入队列中。
228+
229+
最后统计访问过的节点个数,若与节点总数相同则说明可以访问所有节点,否则说明存在无法到达的节点。
230+
231+
时间复杂度 $O(n + m),ドル空间复杂度 $O(n),ドル其中 $n$ 为节点个数,而 $m$ 为边的个数。
232+
233+
<!-- tabs:start -->
234+
235+
#### Python3
236+
237+
```python
238+
class Solution:
239+
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
240+
vis = set()
241+
q = deque([0])
242+
while q:
243+
i = q.popleft()
244+
if i in vis:
245+
continue
246+
vis.add(i)
247+
q.extend(j for j in rooms[i])
248+
return len(vis) == len(rooms)
249+
```
250+
251+
#### Java
252+
253+
```java
254+
class Solution {
255+
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
256+
int n = rooms.size();
257+
boolean[] vis = new boolean[n];
258+
Deque<Integer> q = new ArrayDeque<>();
259+
q.offer(0);
260+
int cnt = 0;
261+
while (!q.isEmpty()) {
262+
int i = q.poll();
263+
if (vis[i]) {
264+
continue;
265+
}
266+
vis[i] = true;
267+
++cnt;
268+
for (int j : rooms.get(i)) {
269+
q.offer(j);
270+
}
271+
}
272+
return cnt == n;
273+
}
274+
}
275+
```
276+
277+
#### C++
278+
279+
```cpp
280+
class Solution {
281+
public:
282+
bool canVisitAllRooms(vector<vector<int>>& rooms) {
283+
int n = rooms.size();
284+
vector<bool> vis(n);
285+
queue<int> q{{0}};
286+
int cnt = 0;
287+
while (q.size()) {
288+
int i = q.front();
289+
q.pop();
290+
if (vis[i]) {
291+
continue;
292+
}
293+
vis[i] = true;
294+
++cnt;
295+
for (int j : rooms[i]) {
296+
q.push(j);
297+
}
298+
}
299+
return cnt == n;
300+
}
301+
};
302+
```
303+
304+
#### Go
305+
306+
```go
307+
func canVisitAllRooms(rooms [][]int) bool {
308+
n := len(rooms)
309+
vis := make([]bool, n)
310+
cnt := 0
311+
q := []int{0}
312+
for len(q) > 0 {
313+
i := q[0]
314+
q = q[1:]
315+
if vis[i] {
316+
continue
317+
}
318+
vis[i] = true
319+
cnt++
320+
for _, j := range rooms[i] {
321+
q = append(q, j)
322+
}
323+
}
324+
return cnt == n
325+
}
326+
```
327+
328+
#### TypeScript
329+
330+
```ts
331+
function canVisitAllRooms(rooms: number[][]): boolean {
332+
const vis = new Set<number>();
333+
const q: number[] = [0];
334+
335+
while (q.length) {
336+
const i = q.pop()!;
337+
if (vis.has(i)) {
338+
continue;
339+
}
340+
vis.add(i);
341+
q.push(...rooms[i]);
342+
}
343+
344+
return vis.size == rooms.length;
345+
}
346+
```
347+
348+
<!-- tabs:end -->
349+
350+
<!-- solution:end -->
351+
221352
<!-- problem:end -->

‎solution/0800-0899/0841.Keys and Rooms/README_EN.md‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,135 @@ impl Solution {
213213

214214
<!-- solution:end -->
215215

216+
<!-- solution:start -->
217+
218+
### Solution 2: BFS
219+
220+
We can also use the Breadth-First Search (BFS) method to traverse the entire graph. We use a hash table or an array `vis` to mark whether the current node has been visited to prevent repeated visits.
221+
222+
Specifically, we define a queue $q,ドル initially put node 0ドル$ into the queue, and then continuously traverse the queue. Each time we take out the front node $i$ of the queue, if $i$ has been visited, we skip it directly; otherwise, we mark it as visited, and then add the nodes that $i$ can reach to the queue.
223+
224+
Finally, we count the number of visited nodes. If it is the same as the total number of nodes, it means that all nodes can be visited; otherwise, it means that there are unreachable nodes.
225+
226+
The time complexity is $O(n + m),ドル and the space complexity is $O(n)$. Where $n$ is the number of nodes, and $m$ is the number of edges.
227+
228+
<!-- tabs:start -->
229+
230+
#### Python3
231+
232+
```python
233+
class Solution:
234+
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
235+
vis = set()
236+
q = deque([0])
237+
while q:
238+
i = q.popleft()
239+
if i in vis:
240+
continue
241+
vis.add(i)
242+
q.extend(j for j in rooms[i])
243+
return len(vis) == len(rooms)
244+
```
245+
246+
#### Java
247+
248+
```java
249+
class Solution {
250+
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
251+
int n = rooms.size();
252+
boolean[] vis = new boolean[n];
253+
Deque<Integer> q = new ArrayDeque<>();
254+
q.offer(0);
255+
int cnt = 0;
256+
while (!q.isEmpty()) {
257+
int i = q.poll();
258+
if (vis[i]) {
259+
continue;
260+
}
261+
vis[i] = true;
262+
++cnt;
263+
for (int j : rooms.get(i)) {
264+
q.offer(j);
265+
}
266+
}
267+
return cnt == n;
268+
}
269+
}
270+
```
271+
272+
#### C++
273+
274+
```cpp
275+
class Solution {
276+
public:
277+
bool canVisitAllRooms(vector<vector<int>>& rooms) {
278+
int n = rooms.size();
279+
vector<bool> vis(n);
280+
queue<int> q{{0}};
281+
int cnt = 0;
282+
while (q.size()) {
283+
int i = q.front();
284+
q.pop();
285+
if (vis[i]) {
286+
continue;
287+
}
288+
vis[i] = true;
289+
++cnt;
290+
for (int j : rooms[i]) {
291+
q.push(j);
292+
}
293+
}
294+
return cnt == n;
295+
}
296+
};
297+
```
298+
299+
#### Go
300+
301+
```go
302+
func canVisitAllRooms(rooms [][]int) bool {
303+
n := len(rooms)
304+
vis := make([]bool, n)
305+
cnt := 0
306+
q := []int{0}
307+
for len(q) > 0 {
308+
i := q[0]
309+
q = q[1:]
310+
if vis[i] {
311+
continue
312+
}
313+
vis[i] = true
314+
cnt++
315+
for _, j := range rooms[i] {
316+
q = append(q, j)
317+
}
318+
}
319+
return cnt == n
320+
}
321+
```
322+
323+
#### TypeScript
324+
325+
```ts
326+
function canVisitAllRooms(rooms: number[][]): boolean {
327+
const vis = new Set<number>();
328+
const q: number[] = [0];
329+
330+
while (q.length) {
331+
const i = q.pop()!;
332+
if (vis.has(i)) {
333+
continue;
334+
}
335+
vis.add(i);
336+
q.push(...rooms[i]);
337+
}
338+
339+
return vis.size == rooms.length;
340+
}
341+
```
342+
343+
<!-- tabs:end -->
344+
345+
<!-- solution:end -->
346+
216347
<!-- problem:end -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
bool canVisitAllRooms(vector<vector<int>>& rooms) {
4+
int n = rooms.size();
5+
vector<bool> vis(n);
6+
queue<int> q{{0}};
7+
int cnt = 0;
8+
while (q.size()) {
9+
int i = q.front();
10+
q.pop();
11+
if (vis[i]) {
12+
continue;
13+
}
14+
vis[i] = true;
15+
++cnt;
16+
for (int j : rooms[i]) {
17+
q.push(j);
18+
}
19+
}
20+
return cnt == n;
21+
}
22+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func canVisitAllRooms(rooms [][]int) bool {
2+
n := len(rooms)
3+
vis := make([]bool, n)
4+
cnt := 0
5+
q := []int{0}
6+
for len(q) > 0 {
7+
i := q[0]
8+
q = q[1:]
9+
if vis[i] {
10+
continue
11+
}
12+
vis[i] = true
13+
cnt++
14+
for _, j := range rooms[i] {
15+
q = append(q, j)
16+
}
17+
}
18+
return cnt == n
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
3+
int n = rooms.size();
4+
boolean[] vis = new boolean[n];
5+
Deque<Integer> q = new ArrayDeque<>();
6+
q.offer(0);
7+
int cnt = 0;
8+
while (!q.isEmpty()) {
9+
int i = q.poll();
10+
if (vis[i]) {
11+
continue;
12+
}
13+
vis[i] = true;
14+
++cnt;
15+
for (int j : rooms.get(i)) {
16+
q.offer(j);
17+
}
18+
}
19+
return cnt == n;
20+
}
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
3+
vis = set()
4+
q = deque([0])
5+
while q:
6+
i = q.popleft()
7+
if i in vis:
8+
continue
9+
vis.add(i)
10+
q.extend(j for j in rooms[i])
11+
return len(vis) == len(rooms)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function canVisitAllRooms(rooms: number[][]): boolean {
2+
const vis = new Set<number>();
3+
const q: number[] = [0];
4+
5+
while (q.length) {
6+
const i = q.pop()!;
7+
if (vis.has(i)) {
8+
continue;
9+
}
10+
vis.add(i);
11+
q.push(...rooms[i]);
12+
}
13+
14+
return vis.size == rooms.length;
15+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /