FInd overlapping appointments in O(n) time?

前端 未结 5 860
独厮守ぢ
独厮守ぢ 2020-12-12 18:16

I was recently asked this question in an interview. Even though I was able to come up the O(n²) solution, the interviewer was obsessed with an O(<

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 19:04

    A naive approach might be to build two parallel trees, one ordered by the beginning point, and one ordered by the ending point of each interval. This allows discarding half of each tree in O(log n) time, but the results must be merged, requiring O(n) time. This gives us queries in O(n + log n) = O(n).

提交回复
热议问题