You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0800-0899/0841.Keys and Rooms/README_EN.md
+57-64
Original file line number
Diff line number
Diff line change
@@ -46,19 +46,25 @@ Since we were able to visit every room, we return true.
46
46
47
47
## Solutions
48
48
49
-
### Solution 1
49
+
### Solution 1: Depth-First Search (DFS)
50
+
51
+
We can use the Depth-First Search (DFS) method to traverse the entire graph, count the number of reachable nodes, and use an array `vis` to mark whether the current node has been visited to prevent repeated visits.
52
+
53
+
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, there are nodes that cannot be reached.
54
+
55
+
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.
0 commit comments