forked from spring1843/go-dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheapest_flights.go
40 lines (35 loc) · 910 Bytes
/
cheapest_flights.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
40
package graph
import "math"
// CheapestFlights solves the problem in O(n^2) time and O(n) space.
func CheapestFlights(flights [][]int, cityCount, source, destination, maxStops int) int {
var (
min = math.MaxInt64
graph = flightGraph(flights, cityCount)
findMinDFS func(vertex, distance, depth int)
)
findMinDFS = func(vertex, distance, depth int) {
if depth > maxStops {
return
}
if vertex == destination && distance < min {
min = distance
}
for _, edge := range graph[vertex] {
if temp := distance + edge[1]; temp < min {
findMinDFS(edge[0], temp, depth+1)
}
}
}
findMinDFS(source, 0, 0)
if min == math.MaxInt64 {
return -1
}
return min
}
func flightGraph(flights [][]int, cityCount int) [][][]int {
graph := make([][][]int, cityCount)
for _, flight := range flights {
graph[flight[0]] = append(graph[flight[0]], flight[1:])
}
return graph
}