-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
40 lines (33 loc) · 1.26 KB
/
Solution.java
File metadata and controls
40 lines (33 loc) · 1.26 KB
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
import java.util.*;
class Solution {
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
Map<Integer, List<int[]>> adj = new HashMap<>();
for (int[] flight : flights) {
adj.computeIfAbsent(flight[0], key -> new ArrayList<>()).add(new int[] {flight[1], flight[2]});
}
int[] dist = new int[n];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[src] = 0;
Queue<int[]> q = new LinkedList<>();
q.offer(new int[] {src, 0});
int stops = 0;
while (!q.isEmpty() && stops <= k) {
int sz = q.size();
while (sz-- > 0) {
int[] curr = q.poll();
int node = curr[0];
int distance = curr[1];
if (!adj.containsKey(node)) continue;
for (int[] next : adj.get(node)) {
int neighbour = next[0];
int price = next[1];
if (price + distance >= dist[neighbour]) continue;
dist[neighbour] = price + distance;
q.offer(new int[] {neighbour, dist[neighbour]});
}
}
stops++;
}
return dist[dst] == Integer.MAX_VALUE ? -1 : dist[dst];
}
}