Problem
There are n cities with the numbers 0 to n - 1. Each item [from, to, price] in flights is a one-way flight from city from to city to. The flight costs price.
Find the cheapest route from city src to city dst that has at most k stops. A stop is a city on the route between src and dst.
Return the cost of the cheapest route. If no such route exists, return -1.
Example 1
- Input:
- n = 4, flights = [[0,1,50],[1,3,50],[0,2,10],[2,1,10],[0,3,200]], src = 0, dst = 3, k = 1
- Output:
- 100
- Explanation:
- The route 0, 2, 1, 3 costs only 70, but it has 2 stops. The cheapest route with at most 1 stop is 0, 1, 3 for 100.
Example 2
- Input:
- n = 4, flights = [[0,1,50],[1,3,50],[0,2,10],[2,1,10],[0,3,200]], src = 0, dst = 3, k = 2
- Output:
- 70
- Explanation:
- Now the route 0, 2, 1, 3 has an allowed number of stops. It costs 10 + 10 + 50, which is 70.
Example 3
- Input:
- n = 3, flights = [[0,1,5]], src = 0, dst = 2, k = 1
- Output:
- -1
- Explanation:
- No flight goes to city 2.
Constraints
- 1 <= n <= 100
- 0 <= flights.length <= n * (n - 1) / 2
- flights[i].length == 3
- 0 <= from, to < n
- from != to
- 1 <= price <= 10^4
- No two flights have the same from and to
- 0 <= src, dst, k < n
- src != dst
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Network Delay Time
Coding · Breadth-First Search · Depth-First Search · Dijkstra's Algorithm
- Evaluate Division
Coding · Array · Bellman–Ford Algorithm · Breadth-First Search
- Minimum Edge Reversals So Every Node Is Reachable
Coding · Breadth-First Search · Depth-First Search · Dynamic Programming
- Alien Dictionary
Coding · Array · Breadth-First Search · Depth-First Search
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 2 records
Illumio
Leetcode 787. Cheapest Flights Within K Stops
Reported Oct 1, 2026 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View sourceMeta
Leetcode 787. Cheapest Flights Within K Stops
Reported Apr 28, 2025 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View source
LeetCode company tags · 6 records
Flipkart
Cheapest Flights Within K Stops
Reported date not specified · Source window: 6mo
Source snapshot: Oct 20, 2026
Source frequency score: 55.4. This is not an interview probability.
View sourceOracle
Cheapest Flights Within K Stops