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 f82c9fc

Browse files
committed
Solve 841. Keys and Rooms
1 parent e44c072 commit f82c9fc

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

‎08/0841-Keys_and_Rooms.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
3+
fun canVisitAllRooms(rooms: List<List<Int>>): Boolean {
4+
val visited = mutableSetOf<Int>()
5+
visit(rooms, visited, 0)
6+
7+
return visited.size == rooms.size
8+
}
9+
10+
private fun visit(rooms: List<List<Int>>, visited: MutableSet<Int>, room: Int) {
11+
visited.add(room)
12+
rooms[room].forEach { nextRoom ->
13+
if (!visited.contains(nextRoom)) {
14+
visit(rooms, visited, nextRoom)
15+
}
16+
}
17+
}
18+
19+
}

0 commit comments

Comments
(0)

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