Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

n =
flights =
src =
dst =
k =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Limit to at most k stops => at most k+1 edges. Use Bellman-Ford style relaxations by edge count.

Approach

dist[i] = best price to i using up to current number of edges. Repeat k+1 rounds:

  • Copy dist to nextDist.
  • Relax all flights using previous dist only. Return dist[dst].

Why it works

Each round adds one more allowed edge; copying prevents using more than that per round.

Code
Loading...
Complexity
Time: O((k+1) * E)
Space: O(V)
Solution
Updated: 2026-02-23

Idea

Limit to at most k stops => at most k+1 edges. Use Bellman-Ford style relaxations by edge count.

Approach

dist[i] = best price to i using up to current number of edges. Repeat k+1 rounds:

  • Copy dist to nextDist.
  • Relax all flights using previous dist only. Return dist[dst].

Why it works

Each round adds one more allowed edge; copying prevents using more than that per round.

Code
Loading...
Complexity
Time: O((k+1) * E)
Space: O(V)