Cheapest Flights Within K Stops
MediumGraphs
Decode it first. Clues, constraints, pattern and plan before any code.There are n cities and a list of one-way flights, where [from, to, price] is a flight costing price. Return the cheapest price to get from src to dst using at most k stops (a stop is a city you land in between src and dst, so at most k + 1 flights). Return -1 if no such route exists.
n = 4, flights = [[0, 1, 100], [1, 2, 100], [2, 0, 100], [1, 3, 600], [2, 3, 200]]
src = 0, dst = 3, k = 1 → 700 (0 → 1 → 3; the cheaper 0 → 1 → 2 → 3 needs 2 stops)
n = 3, flights = [[0, 1, 100], [1, 2, 100], [0, 2, 500]]
src = 0, dst = 2, k = 0 → 500 (no stops allowed, so fly direct)Constraints
1 <= n <= 1000 <= flights.length <= n * (n - 1) / 21 <= price <= 10^40 <= src, dst, k < nandsrc != dst