-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall-paths-from-source-to-target.go
39 lines (32 loc) · 1.07 KB
/
all-paths-from-source-to-target.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import "fmt"
// source: https://leetcode.com/problems/all-paths-from-source-to-target/
func allPathsSourceTarget(graph [][]int) [][]int {
pathsCache := make([][][]int, len(graph))
return findPaths(graph, 0, len(graph)-1, pathsCache)
}
func findPaths(graph [][]int, start, end int, pathsCache [][][]int) (res [][]int) {
if start == end {
return [][]int{{start}}
}
res = make([][]int, 0)
for _, node := range graph[start] {
var nodeToEndPaths [][]int
if nodeToEndPaths = pathsCache[node]; nodeToEndPaths == nil {
nodeToEndPaths = findPaths(graph, node, end, pathsCache)
pathsCache[node] = nodeToEndPaths
}
for _, path := range nodeToEndPaths {
res = append(res, append([]int{start}, path...))
}
}
return res
}
func main() {
// Example 1
var graph1 = [][]int{{1, 2}, {3}, {3}, {}}
fmt.Println("Expected: [[0,1,3],[0,2,3]] Output: ", allPathsSourceTarget(graph1))
// Example 2
var graph2 = [][]int{{4, 3, 1}, {3, 2, 4}, {3}, {4}, {}}
fmt.Println("Expected: [[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]] Output: ", allPathsSourceTarget(graph2))
}