Compare commits
4 Commits
ab90e9c219
...
262f2aae90
Author | SHA1 | Date |
---|---|---|
Bill Ewanick | 262f2aae90 | |
Bill Ewanick | f9192bc541 | |
Bill Ewanick | 454937e2b8 | |
Bill Ewanick | 19b263e7e9 |
|
@ -80,6 +80,7 @@
|
||||||
clean
|
clean
|
||||||
|
|
||||||
go
|
go
|
||||||
|
gofumpt
|
||||||
]);
|
]);
|
||||||
|
|
||||||
shellHook = ''
|
shellHook = ''
|
||||||
|
|
|
@ -3,22 +3,22 @@ package main
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
res := canVisitAllRooms([][]int{{1}, {2}, {}})
|
res := canVisitAllRooms([][]int{{1, 3}, {3, 0, 1}, {2}, {0}})
|
||||||
fmt.Printf("%+v\n", res)
|
fmt.Printf("%+v\n", res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func canVisitAllRooms(rooms [][]int) bool {
|
func canVisitAllRooms(rooms [][]int) bool {
|
||||||
if len(rooms) > 0 && len(rooms[0]) == 0 {
|
if len(rooms) > 0 && len(rooms[0]) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
keys := getAllKeys(0, make(map[int]struct{}), rooms)
|
keys := getAllKeys(0, make(map[int]struct{}), rooms)
|
||||||
keys[0] = struct{}{}
|
keys[0] = struct{}{}
|
||||||
|
|
||||||
for i := 0; i < len(rooms); i++ {
|
for i := 0; i < len(rooms); i++ {
|
||||||
if _, ok := keys[i]; !ok {
|
if _, ok := keys[i]; !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -27,15 +27,17 @@ func getAllKeys(index int, seen map[int]struct{}, rooms [][]int) map[int]struct{
|
||||||
keys := map[int]struct{}{}
|
keys := map[int]struct{}{}
|
||||||
|
|
||||||
for j := 0; j < len(rooms[index]); j++ {
|
for j := 0; j < len(rooms[index]); j++ {
|
||||||
if _, ok := keys[rooms[index][j]]; ok {
|
if _, ok := seen[rooms[index][j]]; ok {
|
||||||
continue
|
continue
|
||||||
}
|
} else {
|
||||||
keys[rooms[index][j]] = struct{}{}
|
seen[rooms[index][j]] = struct{}{}
|
||||||
insideKeys := getAllKeys(j, keys, rooms)
|
}
|
||||||
for k := range insideKeys {
|
keys[rooms[index][j]] = struct{}{}
|
||||||
keys[k] = struct{}{}
|
insideKeys := getAllKeys(rooms[index][j], seen, rooms)
|
||||||
}
|
for k := range insideKeys {
|
||||||
|
keys[k] = struct{}{}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue