Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

intervals =
queries =

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

For each query, consider all intervals that start <= query; among those that also end >= query, pick smallest length.

Approach

Sort intervals by start. Sort queries with original indices. Sweep queries increasing:

  • Push intervals with start <= q into a min-heap keyed by (length, end).
  • Pop heap entries whose end < q.
  • Heap top gives minimal interval covering q.

Why it works

At query time, heap contains exactly candidates that could cover current q, and min-heap ensures smallest length is chosen.

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

Idea

For each query, consider all intervals that start <= query; among those that also end >= query, pick smallest length.

Approach

Sort intervals by start. Sort queries with original indices. Sweep queries increasing:

  • Push intervals with start <= q into a min-heap keyed by (length, end).
  • Pop heap entries whose end < q.
  • Heap top gives minimal interval covering q.

Why it works

At query time, heap contains exactly candidates that could cover current q, and min-heap ensures smallest length is chosen.

Code
Loading...
Complexity
Time: O((n+m) log n)
Space: O(n)