Find the Cheapest Flight Route with a Stop Limit

Quick Overview

Find the Cheapest Flight Route with a Stop Limit. Each flights row [from,to,price] is a directed flight.

Find the Cheapest Flight Route with a Stop Limit

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Each flights row [from,to,price] is a directed flight. Return the cheapest total fare from src to dst using at most k intermediate stops, or -1 when no qualifying route exists. A route with k stops uses at most k+1 flights. ```cpp class Solution { public: int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k); }; ``` ### Examples **Example 1:** ```text n = 3, flights = [[0,1,4],[1,2,5],[0,2,12]], src = 0, dst = 2, k = 1 Output: 9 Explanation: The two-flight route costs4+5. ``` **Example 2:** ```text n = 3, flights = [[0,1,4],[1,2,5],[0,2,12]], src = 0, dst = 2, k = 0 Output: 12 Explanation: Only the direct flight fits the zero-stop limit. ``` ### Constraints - City count n is 2 through 100. - There are 0 through n*(n-1)/2 flight rows, each of length 3. - Endpoints are different labels in [0,n-1], and directed endpoint pairs do not repeat. - Fares lie in [1,10000]. - src,dst,k belong to [0,n-1], and src differs from dst. ### Contract considerations Preserve direction and distinguish stops from flight edges. ```hint Read the boundary condition Why must every relaxation in a layer read the previous layer? ``` ### Boundary reminders Preserve direction and distinguish stops from flight edges.

Overview: Find the Cheapest Flight Route with a Stop Limit. Each flights row [from,to,price] is a directed flight.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Each flights row [from,to,price] is a directed flight. Return the cheapest total fare from src to dst using at most k intermediate stops, or -1 when no qualifying route exists. A route with k stops uses at most k+1 flights.

class Solution {
public:
    int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k);
};

Examples

Example 1:

n = 3, flights = [[0,1,4],[1,2,5],[0,2,12]], src = 0, dst = 2, k = 1
Output: 9
Explanation: The two-flight route costs4+5.

Example 2:

n = 3, flights = [[0,1,4],[1,2,5],[0,2,12]], src = 0, dst = 2, k = 0
Output: 12
Explanation: Only the direct flight fits the zero-stop limit.

Constraints

  • City count n is 2 through 100.
  • There are 0 through n*(n-1)/2 flight rows, each of length 3.
  • Endpoints are different labels in [0,n-1], and directed endpoint pairs do not repeat.
  • Fares lie in [1,10000].
  • src,dst,k belong to [0,n-1], and src differs from dst.

Contract considerations

Preserve direction and distinguish stops from flight edges.

Boundary reminders

Preserve direction and distinguish stops from flight edges.