Cheapest flight within stops
Given n cities (0..n-1) and a list of directed flights [from, to, price], find the cheapest total price to travel from src to dst using at most k intermediate stops (so at most k+1 flights). Return the cheapest price, or -1 if no route within the stop limit exists. Prices are non-negative integers.
Implement
cheapest_k_stops(n: int, flights: list[list[int]], src: int, dst: int, k: int) → intExamples
in
[4,[[0,1,100],[1,2,100],[2,3,100],[0,3,500]],0,3,1]out500What 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
[4,[[0,1,100],[1,2,100],[2,3,100],[0,3,500]],0,3,1]500not run yetsample