Leetcode Problem 1851. Minimum Interval to Include Each Query

1851. Minimum Interval to Include Each Query

Leetcode Solutions

Priority Queue Approach

  1. Sort the intervals by their starting points.
  2. Sort the queries and maintain their original indices for the result array.
  3. Initialize a priority queue (min-heap) to store intervals, ordered by interval size and then by ending point.
  4. Iterate through the sorted queries: a. Add all intervals to the priority queue that start before or at the current query point. b. Remove intervals from the priority queue that end before the current query point. c. If the priority queue is not empty, the top element's size is the answer for the current query; otherwise, the answer is -1.
  5. Store the answers in the result array using the original indices of the queries.
  6. Return the result array.
UML Thumbnail

Binary Search Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...