Gamified challenge · Bellman-Ford
There are n cities, 0 to n - 1. Each entry [from, to, price] of flights is a one-way flight. You start in src and want to reach dst with at most `k` layovers, that is at most k + 1 flights.
Write cheapest(n, flights, src, dst, k) that returns the lowest total price of such a trip, or -1 if there is none. If src == dst the trip is free: return 0.
Example: flights [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]] from 0 to 3. With k = 1 the best is 0 → 1 → 3 for 700; with k = 2, 0 → 1 → 2 → 3 costs 400.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google