Cheapest Flights Within K Stops

You are given n cities connected by flights. Each flight has a starting city, a destination city, and a cost. Given an array of these flights, along with three integers: the starting city (src), the destination city (dst), and the maximum number of stops (k), return the cheapest price for a flight from src to dst with at most k stops. If no such route exists, return -1.

Constraints

  • 1 <= n <= 100
  • 0 <= flights.length <= (n * (n - 1) / 2)
  • flights[i].length == 3
  • 0 <= from_i, to_i < n
  • from_i != to_i
  • 1 <= price_i <= 10^4
  • There will not be any multiple flights between two cities.
  • 0 <= src, dst, k < n
  • src != dst
Saving… · 2 lines
class Solution:
def cheapestFlightsWithinKStops(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
Console

Click Run to execute sample tests.