aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorterminaldweller <thabogre@gmail.com>2022-12-20 05:17:57 +0000
committerterminaldweller <thabogre@gmail.com>2022-12-20 05:17:57 +0000
commit885550225720f1141a80ca6dc7ea550bba20afa8 (patch)
treed2cda2e6b8beb1ac09f9376dcde1034940bfcd80
parentupdate (diff)
downloadleetcode-885550225720f1141a80ca6dc7ea550bba20afa8.tar.gz
leetcode-885550225720f1141a80ca6dc7ea550bba20afa8.zip
841
-rw-r--r--841/go.mod3
-rw-r--r--841/main.go26
2 files changed, 29 insertions, 0 deletions
diff --git a/841/go.mod b/841/go.mod
new file mode 100644
index 0000000..d38aff7
--- /dev/null
+++ b/841/go.mod
@@ -0,0 +1,3 @@
+module 841
+
+go 1.19
diff --git a/841/main.go b/841/main.go
new file mode 100644
index 0000000..586f0fd
--- /dev/null
+++ b/841/main.go
@@ -0,0 +1,26 @@
+package main
+
+import "fmt"
+
+func canVisitAllRooms(rooms [][]int) bool {
+ visited := map[int]bool{}
+ dfs(rooms, visited, 0)
+ return len(visited) == len(rooms)
+}
+
+func dfs(rooms [][]int, visited map[int]bool, room int) {
+ if _, ok := visited[room]; !ok {
+ visited[room] = true
+ } else {
+ return
+ }
+
+ for i := range rooms[room] {
+ dfs(rooms, visited, rooms[room][i])
+ }
+}
+
+func main() {
+ fmt.Println(canVisitAllRooms([][]int{{1}, {2}, {3}, {}}))
+ fmt.Println(canVisitAllRooms([][]int{{1, 3}, {3, 0, 1}, {2}, {}}))
+}