Problem 360895 · medium · Phase 03 Linear Management & Searching

Which Interval Contains the Point?

bisect module · intervals

intervals is a list of non-overlapping closed intervals [s, e] sorted by start. For each p in points, return the index of the interval containing p (s <= p <= e), or -1 if none does.

Examples

Input:  intervals = [[1, 3], [5, 8], [10, 10]], points = [2, 4, 10, 9, 0]
Output: [0, -1, 2, -1, -1]

Constraints

  • 0 <= len(intervals) <= 10**5, 0 <= len(points) <= 10**5
  • -10**9 <= s <= e <= 10**9
  • Required time: O(q log n).

Goals

  • Binary search on interval starts with bisect
  • Verify the candidate interval's end before accepting it
Starting Python…