How to find the cheapest flight within K stops
Cheapest Flights Within K Stops
Given n cities (numbered 0 to n-1) and a list of flights, where each flights[i] = [u, v, price] represents a directed flight from city u to city v with a cost price (guaranteed price > 0).
Your task is to compute the minimum total price to travel from a source city (src) to a destination city (dst) with at most k stops. This implies a maximum of k+1 flight segments.
Output
Return the minimum price. If it is impossible to reach the destination within the given stop limit, return -1.
Constraints / Details
- Flights are directed edges (
u -> v). - Multiple distinct flights may connect the same pair of cities.
Follow-up
Consider a scenario where the airline network is extremely large (both n and the number of flights are substantial). Additionally, the system needs to support real-time ticket price updates and handle frequent queries efficiently. How would you optimize the overall solution for such a scale and dynamic environment?
AI interview prep
Core dilemma
Balancing the need for optimal pathfinding (minimum cost) with a hard constraint on the number of intermediate stops, especially when considering real-time updates and large-scale, dynamic graph data in the follow-up.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
How to find the cheapest flight within K stops
Cheapest Flights Within K Stops
Given n cities (numbered 0 to n-1) and a list of flights, where each flights[i] = [u, v, price] represents a directed flight from city u to city v with a cost price (guaranteed price > 0).
Your task is to compute the minimum total price to travel from a source city (src) to a destination city (dst) with at most k stops. This implies a maximum of k+1 flight segments.
Output
Return the minimum price. If it is impossible to reach the destination within the given stop limit, return -1.
Constraints / Details
- Flights are directed edges (
u -> v). - Multiple distinct flights may connect the same pair of cities.
Follow-up
Consider a scenario where the airline network is extremely large (both n and the number of flights are substantial). Additionally, the system needs to support real-time ticket price updates and handle frequent queries efficiently. How would you optimize the overall solution for such a scale and dynamic environment?