-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheapest_Flights_Within_K_Stops.java
More file actions
43 lines (37 loc) · 1.47 KB
/
Cheapest_Flights_Within_K_Stops.java
File metadata and controls
43 lines (37 loc) · 1.47 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
41
42
43
import java.util.*;
import java.io.*;
import java.lang.*;
public class Cheapest_Flights_Within_K_Stops {
class Solution {
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
Map<Integer, List<int[]>> hm = new HashMap<>();
for (int[] flight : flights) {
hm.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[]> qu = new LinkedList<>();
qu.offer(new int[] {src, 0});
int stops = 0;
while (!qu.isEmpty() && stops <= k) {
int sz = qu.size();
while (sz-- > 0) {
int[] curr = qu.poll();
int node = curr[0];
int distance = curr[1];
if (!hm.containsKey(node)) continue;
for (int[] next : hm.get(node)) {
int neighbour = next[0];
int price = next[1];
if (price + distance >= dist[neighbour]) continue;
dist[neighbour] = price + distance;
qu.offer(new int[] {neighbour, dist[neighbour]});
}
}
stops++;
}
return dist[dst] == Integer.MAX_VALUE ? -1 : dist[dst];
}
}
}