Problem
Ask
Submissions

Problem: Cheapest Flights Within K Stops

Medium
30 min
Explore how to solve the problem of finding the cheapest flight route from a source city to a destination city with at most k stops. This lesson teaches you to apply dynamic programming to efficiently compute the minimum travel cost, understand constraints, and implement a solution that handles complex optimization within set limits.

Statement

You are given n cities, numbered from 00 to n 1- 1 connected by several flights. You are also given an array flights, where each flight is represented as flights[i] =[fromi,toi,pricei]= [{from}_i, {to}_i, {price}_i] meaning there is a direct flight from city fromfromᵢ to city totoᵢ with a cost of pricepriceᵢ.

You are also given three integers:

  • src: The starting city.

  • dst: The destination city.

  • k: The maximum number of stops allowed on the route (i.e., intermediate cities between src and dst).

Your task is to find the minimum possible cost to travel from src to dst using at most k stops (i.e., the route may contain up to k + 1 flights). If there is no valid route from src to dst that uses at most k stops, return 1-1.

Constraints:

  • 22 \leq n \leq 100100

  • 00 \leq flights.length (n×(n1)/2)\leq (n \times (n - 1) / 2)

  • flights[i].length ==3== 3

  • 0fromi0 \leq {from}_i , toi<n{to}_i < n

  • fromitoi{from}_i \ne {to}_i

  • 1pricei1041 \leq {price}_i \leq 10^4

  • There will not be any multiple flights between two cities.

  • 00 \leq src, dst, k <n< n

  • src \ne dst

Problem
Ask
Submissions

Problem: Cheapest Flights Within K Stops

Medium
30 min
Explore how to solve the problem of finding the cheapest flight route from a source city to a destination city with at most k stops. This lesson teaches you to apply dynamic programming to efficiently compute the minimum travel cost, understand constraints, and implement a solution that handles complex optimization within set limits.

Statement

You are given n cities, numbered from 00 to n 1- 1 connected by several flights. You are also given an array flights, where each flight is represented as flights[i] =[fromi,toi,pricei]= [{from}_i, {to}_i, {price}_i] meaning there is a direct flight from city fromfromᵢ to city totoᵢ with a cost of pricepriceᵢ.

You are also given three integers:

  • src: The starting city.

  • dst: The destination city.

  • k: The maximum number of stops allowed on the route (i.e., intermediate cities between src and dst).

Your task is to find the minimum possible cost to travel from src to dst using at most k stops (i.e., the route may contain up to k + 1 flights). If there is no valid route from src to dst that uses at most k stops, return 1-1.

Constraints:

  • 22 \leq n \leq 100100

  • 00 \leq flights.length (n×(n1)/2)\leq (n \times (n - 1) / 2)

  • flights[i].length ==3== 3

  • 0fromi0 \leq {from}_i , toi<n{to}_i < n

  • fromitoi{from}_i \ne {to}_i

  • 1pricei1041 \leq {price}_i \leq 10^4

  • There will not be any multiple flights between two cities.

  • 00 \leq src, dst, k <n< n

  • src \ne dst