◀ THE GRIND — ADVANCED GRAPHS

Cheapest Flights Within K Stops

MEDIUM✓ CHIP-TIMEDLC #787 — FULL STATEMENT ↗

The drill: Flights connect cities with a price each. Find the cheapest way from a source city to a destination that uses at most k layovers — cheapest isn't always the route with the fewest hops.

THE BRIEFING — THE FULL DRILL, IN MY OWN WORDS

A set of one-way flights arrives, each with its own price, along with a source city, a destination city, and a maximum number of layovers allowed along the way.

A route qualifies only if it uses at most that many layovers — one layover fewer than the number of flights taken. Among every qualifying route, the task is to find the cheapest total price.

If no route from the source to the destination fits inside the layover budget, that has to be signaled distinctly rather than treated as a price of zero.

EX 01
n = 3 · flights = [[0, 1, 100], [1, 2, 100], [0, 2, 500]] · src = 0 · dst = 2 · k = 1
200
ONE STOP UNLOCKS THE CHEAPER RELAY
EX 02
n = 3 · flights = [[0, 1, 100], [1, 2, 100], [0, 2, 500]] · src = 0 · dst = 2 · k = 0
500
ZERO STOPS FORCES THE DIRECT, PRICIER FLIGHT
EX 03
n = 3 · flights = [[0, 1, 10]] · src = 0 · dst = 2 · k = 5
-1
DESTINATION IS SIMPLY UNREACHABLE
THE HINTS — TAKE ONLY WHAT YOU NEED
HINT 1 THE NUDGE

Plain shortest-path finds the globally cheapest route, full stop — but the cheapest route overall might need more layovers than the budget allows. The stop count is a hard constraint on the search, not a tiebreaker.

HINT 2 THE STRUCTURE

Think in rounds instead of a running frontier: after round i, you know the cheapest way to reach every city using at most i flights. Round i+1 only ever extends those, one more flight each.

HINT 3 ONE STEP FROM THE ANSWER

Bellman-Ford, capped at k+1 rounds: each round, relax every flight edge against LAST round's prices — never this round's half-updated ones, or a city could sneak in an extra hop for free.

COACH'S BOARD — THE PATTERN, STEP BY STEP
PRICES, FROZEN BY THE ROUNDPATTERN · BELLMAN-FORD, K+1 ROUNDSflights: 0→1(100) 1→2(100) 0→2(500) · src=0 dst=2 k=1
0
STEP 1

Row 0: node 0 starts at cost 0, every other node unreached. Each round below allows exactly one more flight.

STEP 1 / 6 · ← → WORK TOO
THE SPLITS — TWO PACES, TWO LANGUAGES
grind/cheapest-flights-within-k-stops.pyRACE PACE
LANG ▸
PACE ▸
class Solution:
    def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
        dist = [float('inf')] * n
        dist[src] = 0
        for _ in range(k + 1):
            new_dist = dist[:]
            for u, v, w in flights:
                if dist[u] != float('inf') and dist[u] + w < new_dist[v]:
                    new_dist[v] = dist[u] + w
            dist = new_dist
        return dist[dst] if dist[dst] != float('inf') else -1
TIME O(K·E)SPACE O(V)PYTHON · RACE PACE · 11 LN

✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED