Cheapest Flights Within K Stops LeetCode solution in Python

1
class Solution:
2
def findCheapestPrice(
3
self, n: int, flights: List[List[int]], src: int, dst: int, k: int
4
) -> int:
5
graph = defaultdict(list)
6
for u, v, w in flights:
7
graph[u].append((v, w))
8

9
pq = [(0, src, 0)]
10
dis = [float("inf")] * n
11

12
while pq:
13
c, n, l = heappop(pq)
14
if n == dst:
15
return c
16
if l > k or l >= dis[n]:
17
continue
18
dis[n] = l
19
for v, w in graph[n]:
20
heappush(pq, (c + w, v, l + 1))
21
return -1

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0