The Runtime Theory
mediumLeetCode#bellman-ford#bounded-relaxation

Cheapest Flights Within K Stops

Find the cheapest route using at most k intermediate stops, or return -1.

The Runtime Theory Team25 min read
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

inn=4, flights=[[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src=0, dst=3, k=1

out700

inn=3, flights=[[0,1,100],[1,2,100],[0,2,500]], src=0, dst=2, k=1

out200

inn=3, flights=[[0,1,100],[1,2,100]], src=0, dst=2, k=0

out-1

Find the cheapest route using at most k intermediate stops, or return -1.

A good solution should

  • State the invariant or decision rule that makes the approach correct.
  • Explain time and space costs in terms of input size and required output.
  • Handle empty, minimal, duplicate, and boundary-shaped inputs.

Reasoning prompt

Bound relaxations by edge count or track stops in the state. An unconstrained shortest-path algorithm can use too many edges.

Use the linked judge for the canonical challenge. The examples above are compact TRT checks; create additional tests around the boundary most likely to break your invariant.

More practice in this topic

One dispatch a week

The trace behind each problem, the tradeoff that explains it, and one technical dispatch per week — no noise.

One technical dispatch per week. No noise.

Not started

Sign in to save your learning progress.

Sign in to save