Cheapest Flights Within K Stops
Flights are directed [from,to,price] edges. Return the lowest total price from src to dst using at most k intermediate stops (at most k+1 edges), or -1 if impossible.
Example 1
n = 3
flights = [[0,1,20],[1,2,30],[0,2,90]]
src = 0
dst = 2
k = 150Explanation
One stop allows 0->1->2 for 20+30=50, beating the direct cost 90.
Example 2
n = 3
flights = [[0,1,20],[1,2,30],[0,2,90]]
src = 0
dst = 2
k = 090Explanation
Zero stops permits only the direct flight costing 90.
Example 3
n = 3
flights = [[0,1,10]]
src = 0
dst = 2
k = 2-1Explanation
No flight path reaches node 2 under any stop allowance.
Constraints
- 2 <= n <= 100
- Node IDs are 0..n-1
- Positive prices <= 10000
- 0 <= k < n
- src differs from dst
Function contract
findCheapestPriceArguments: json, json, json, json, jsonSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 35 cases, on your device.
3[[0,1,20],[1,2,30],[0,2,90]]021