Skip to main content
  1. LeetCode/

LeetCode 787: Cheapest Flights Within K Stops

·3 mins· ·
LeetCode Medium Graph Bellman-Ford Shortest-Path
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-04-25 Source: Day 11 learning note

Study Context
#

This article is rebuilt from the exact LeetCode section in the learning note. I kept the note’s repair points, comparison points, and common mistakes, while removing unrelated non-LeetCode material from the same day.

Related Reminders From The Note#

    1. Explain why LC 787 cannot use plain visited = set(city).

Learning Note Extract
#

Problem 3 - LC 787 Cheapest Flights Within K Stops Review
#

  • Status: Partial repair only; full Bellman-Ford practice deferred.
  • Current understanding: Plain Dijkstra with visited = set(city) is wrong.

Why Plain visited = set(city) Is Wrong
#

The state is not only the city.

Reaching the same city with:

lower price but too many stops

can be worse than:

higher price but fewer stops used

because the second route may leave enough stop budget for a cheaper final path.

So the state must include:

(city, stops_used)

or:

(city, edges_used)

Week 3 Decision
#

Do not force Bellman-Ford learning in a rushed way.

Move full Bellman-Ford intro and full LC 787 practice to:

Week 3 Weekend Day 2

Organized Notes
#

The note correctly flags the trap: city-only visited is not a valid Dijkstra pruning key because remaining stop budget is part of the state. A robust interview answer is Bellman-Ford by edge count. With at most k stops, the path may use at most k + 1 flights, so we relax all flights exactly k + 1 rounds. Each round reads from the previous distance array and writes to a copied array, which prevents one round from using more than one extra flight.

Clean Solution
#

The note above captures the reasoning and the mistakes to avoid. The implementation below is the version I would submit.

from typing import List

class Solution:
    def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
        inf = float('inf')
        dist = [inf] * n
        dist[src] = 0

        for _ in range(k + 1):
            ndist = dist[:]
            for u, v, price in flights:
                if dist[u] != inf and dist[u] + price < ndist[v]:
                    ndist[v] = dist[u] + price
            dist = ndist

        return -1 if dist[dst] == inf else dist[dst]

Complexity
#

Time O((K+1)*E), Space O(V).

Mistakes To Watch
#

  • Using city-only visited in Dijkstra and pruning cheaper paths with more stops incorrectly.
  • Doing K rather than K+1 edge layers.

Final Interview Explanation
#

I would frame this as shortest path with an edge-count limit. Since at most k stops means at most k + 1 flights, I relax all flights for k + 1 layers. Copying the previous distance array each layer guarantees each round uses only one additional flight.

Related

LeetCode 1631: Path With Minimum Effort
·3 mins
LeetCode Medium Graph Dijkstra Shortest-Path
LeetCode note for Path With Minimum Effort, rebuilt from the original learning note
LeetCode 802: Find Eventual Safe States
·3 mins
LeetCode Medium Graph Topological-Sort
LeetCode note for Find Eventual Safe States, rebuilt from the original learning note
LeetCode 851: Loud and Rich
·2 mins
LeetCode Medium Graph Topological-Sort
LeetCode note for Loud and Rich, rebuilt from the original learning note
LeetCode 2115: Find All Possible Recipes from Given Supplies
·3 mins
LeetCode Medium Graph Topological-Sort
LeetCode note for Find All Possible Recipes from Given Supplies, rebuilt from the original learning note
LeetCode 207: Course Schedule
·2 mins
LeetCode Medium Graph Topological-Sort
LeetCode note for Course Schedule, rebuilt from the original learning note
LeetCode 210: Course Schedule II
·2 mins
LeetCode Medium Graph Topological-Sort
LeetCode note for Course Schedule II, rebuilt from the original learning note