Code RoomCheapest flights k stops
MediumPrep Room Coding #1488

Cheapest flights k stops

CodingAlgorithms & data structuresMid–Senior~30 min

There are n cities (0..n-1) connected by directed flights, where flights[i] = [from, to, price]. Given a source src, a destination dst, and an integer k, return the cheapest price to fly from src to dst using at most k intermediate stops (i.e. at most k+1 flights). If there is no such route, return -1.

Implement
cheapest_flights_k_stops(n: int, flights: list[list[int]], src: int, dst: int, k: int) → int
Examples
in[4,[[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]],0,3,1]out700
What 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
InputExpectedGot
[4,[[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]],0,3,1]700not run yetsample