Tensorum
LeetCode 787 · Shortest Path / BFS-Dijkstra

Cheapest Flights Within K Stops

LeetCode 787 · Medium · solution, intuition & step-by-step walkthrough

Heap on (cost, node, stops); skip when stops > k or stops ≥ fewest stops already seen at that node.

MediumDynamic ProgrammingDepth-First SearchBreadth-First SearchGraph TheoryHeap (Priority Queue)
6 msruntime · beats 71%
21.2 MBmemory · beats 15%
1approach
8accepted runs · last 21 Mar 2026

This is how I solved Cheapest Flights Within K Stops (LeetCode 787), a medium problem from the Shortest Path / BFS-Dijkstra pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.

Intuition

Plain Dijkstra finds the cheapest route but ignores how many stops it uses. Put the stop count into the heap entries and prune: a node is worth expanding again only if we reach it with fewer stops than before, because a cheaper-but-longer path seen earlier may run out of stops.

Approach

  1. 1

    Build an adjacency list from the flights.

  2. 2

    Push (0, src, 0) onto a min-heap.

  3. 3

    Pop the cheapest; if it's dst, that cost is the answer.

  4. 4

    Skip entries that used too many stops or more stops than an earlier visit.

  5. 5

    Otherwise record the stops and push every neighbour with cost + price and stops + 1.

How I solved it

Dijkstra with stop pruningPythonO(E·k log) time
class Solution:
    def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
        #we need to minimize cost
        min_heap =[(0,src,0)]#cost,node,stops
        adj = defaultdict(list)
        for u,v,w in flights:
            adj[u].append((v,w))
        stops = [float("inf")]*n
        stops[src]=0
        while min_heap:
            cost,node,steps=heapq.heappop(min_heap)
            if node==dst:
                return cost
            #if we can't reach in k stops or earlier we found a better path with less no of stops
            #we can skip
            if steps>k or steps>stops[node]:
                continue
            stops[node]=steps
            for adj_node,wt in adj[node]:
                heapq.heappush(min_heap,(cost+wt,adj_node,steps+1))
        return -1
Loading the step-through…

Cheapest Flights Within K Stops — FAQ

What is the intuition behind Cheapest Flights Within K Stops?

Plain Dijkstra finds the cheapest route but ignores how many stops it uses. Put the stop count into the heap entries and prune: a node is worth expanding again only if we reach it with fewer stops than before, because a cheaper-but-longer path seen earlier may run out of stops.

How do you solve Cheapest Flights Within K Stops (LeetCode 787)?

1. Build an adjacency list from the flights. 2. Push (0, src, 0) onto a min-heap. 3. Pop the cheapest; if it's dst, that cost is the answer. 4. Skip entries that used too many stops or more stops than an earlier visit. 5. Otherwise record the stops and push every neighbour with cost + price and stops + 1.

What is the time and space complexity of Cheapest Flights Within K Stops?

Time: O(E · k · log(E · k)). Space: O(E · k). (Accepted approach: Dijkstra with stop pruning.)

What are common mistakes in Cheapest Flights Within K Stops?

Standard 'visited' marking breaks this problem — a pricier path with fewer stops may be the only valid one. k stops means k + 1 flights.