1

Given a set of intervals I, each element of the form [a_i, b_i], find an end point b_i of maximum depth in O(n*logn) time. Define depth of x as the number of intervals the point "stabs" (or intersects). If two end point have the same depth, return the smaller one.


Attempt:

I have no idea how to find it in O(n * logn) time. I understand the greedy algorithm for finding the stabbing set of a set of intervals, but finding an end point with strictly O(n * log n) time seems very different.

I can try and first sort the intervals and brute force a maximum depth end point but that does not guarantee O(n * log n) time.

bli00
  • 2,215
  • 2
  • 19
  • 46

1 Answers1

3

You can try the following:

  • sort your points a_i and b_i (together in one array)
  • traversing the sorted array increase a counter (depth) when you encounter an "a" point (starting an interval) and decreases it for "b" point (end of an interval) - here you can find your "b" point with maximum depth
algrid
  • 5,600
  • 3
  • 34
  • 37
  • Correct, but you have to be careful to to sort an "a" before a "b" if they have the same value. For example: [0, 1], [1, 2] -- the maximum depth is 2, which means you have to sort the numbers as [a0, a1, b1, b2] and not [a0, b1, a1, b2]. – Paul Hankin Oct 07 '17 at 06:41