Cheapest flight with stops
There are n cities (0..n-1) and a list of one-way flights [u, v, w] with positive price w. Find the cheapest price to travel from a source city to a destination city using at most k intermediate stops (so at most k+1 flights). Return the cheapest total price, or -1 if it is unreachable within the stop limit. If source equals destination the price is 0. Constraints: 1 <= n <= 200; 0 <= k <= n.
Implement
cheapest_k_stops(n: int, flights: list[list[int]], src: int, dst: int, k: int) → intExamples
in
[3,[[0,1,100],[1,2,100],[0,2,500]],0,2,1]out200What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 30 min
solution.py
InputExpectedGot
[3,[[0,1,100],[1,2,100],[0,2,500]],0,2,1]200not run yetsample