Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

tickets =

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

Find an Eulerian path that uses all tickets exactly once, with lexicographically smallest choice.

Approach

Hierholzer’s algorithm:

  • Build adjacency lists.
  • Use a min-heap for each origin to always take smallest next airport.
  • DFS: while outgoing edges exist, take smallest edge and recurse.
  • Append airport to result on backtrack; reverse at end.

Why it works

Hierholzer constructs an Eulerian trail by stitching cycles; using min-heaps ensures the lexicographically smallest valid itinerary.

Code
Loading...
Complexity
Time: O(E log E)
Space: O(E)
Solution
Updated: 2026-02-23

Idea

Find an Eulerian path that uses all tickets exactly once, with lexicographically smallest choice.

Approach

Hierholzer’s algorithm:

  • Build adjacency lists.
  • Use a min-heap for each origin to always take smallest next airport.
  • DFS: while outgoing edges exist, take smallest edge and recurse.
  • Append airport to result on backtrack; reverse at end.

Why it works

Hierholzer constructs an Eulerian trail by stitching cycles; using min-heaps ensures the lexicographically smallest valid itinerary.

Code
Loading...
Complexity
Time: O(E log E)
Space: O(E)