-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path787. Cheapest Flights Within K Stops.java
92 lines (73 loc) · 2.8 KB
/
787. Cheapest Flights Within K Stops.java
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class Solution {
private static class Node implements Comparable<Node> {
int steps;
int src;
int cost;
public Node(int steps, int src, int cost) {
this.steps = steps;
this.src = src;
this.cost = cost;
}
@Override
public int compareTo(Node o) {
return o.steps - this.steps;
}
@Override
public String toString() {
return "Node [steps=" + steps + ", src=" + src + ", cost=" + cost + "]";
}
}
public int findCheapestPrice(int totalFlights, int[][] flights, int src, int dst, int k) {
// TC: O(E log V * # flights) where E is the number of edges and V is the number
// of
// vertices.
// SC: O(V) because of distance map and visited map.
/**
* There are n cities and m edges connected by some number of flights. You are
* given an array of flights where flights[i] = [ fromi, toi, pricei] indicates
* that there is a flight from city fromi to city toi with cost price. You have
* also given three integers src, dst, and k, and return the cheapest price from
* src to dst with at most k stops. If there is no such route, return -1.
*/
List<Node>[] graph = new List[totalFlights];
for (int i = 0; i < totalFlights; i++) {
graph[i] = new ArrayList<>();
}
// build the graph
for (int[] flight : flights) {
int u = flight[0];
int v = flight[1];
int w = flight[2];
graph[u].add(new Node(0, v, w));
}
// declare the linked list.
Queue<Node> pq = new LinkedList<>();
// declare the visited list
boolean[] visitedMap = new boolean[totalFlights];
int[] distanceMap = new int[totalFlights];
// fill the distanceMap with 1e9
Arrays.fill(distanceMap, Integer.MAX_VALUE);
// add the src into pq
pq.offer(new Node(0, src, 0));
// perform dijsktra using Queue
while (pq.isEmpty() == false) {
Node node = pq.poll();
// System.out.println("visit:\t" + node);
if (node.steps > k)
continue;
// explore all neighbours
for (Node _edge : graph[node.src]) {
// edge, edgeWeight
int edge = _edge.src;
int edgWt = _edge.cost;
if (node.cost + edgWt < distanceMap[edge] && node.steps <= k) {
distanceMap[edge] = node.cost + edgWt;
pq.offer(new Node(node.steps + 1, edge, distanceMap[edge]));
}
}
}
if (distanceMap[dst] == Integer.MAX_VALUE)
return -1;
return distanceMap[dst];
}
}