For an interval we want to find the lowest
such that
.
Sort and search
Keeping the start times sorted let us find efficiently with binary-search.
Time: , space:
.
class Solution:
def find_insert_index(self, end, sorted_starts):
lo, hi = 0, len(sorted_starts) - 1
while lo <= hi:
mid = (lo + hi) // 2
mid_start, _ = sorted_starts[mid]
if mid_start < end:
lo = mid + 1
else:
hi = mid - 1
return sorted_starts[lo][1] if lo < len(sorted_starts) else -1
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
sorted_starts = sorted(
[(interval[0], i) for i, interval in enumerate(intervals)]
)
right_indices = []
for start, end in intervals:
right_indices.append(self.find_insert_index(end, sorted_starts))
return right_indices
Heaps
Say, in the sorted start-times, we are searching right indices in order of end-times (smaller to bigger). If does not work for
, that start-time will not work for other end-times and we can get rid of
from all future searches.
To do that, we keep two min heaps: (1) contains start-times (2) contains end-times.
Time: , space:
.
from heapq import heapify, heappop as pop
class Solution:
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
start_q = [(interval[0], i) for i, interval in enumerate(intervals)]
heapify(start_q)
end_q = [(interval[1], i) for i, interval in enumerate(intervals)]
heapify(end_q)
right_indices = [-1] * len(intervals)
while end_q:
while start_q and end_q[0][0] > start_q[0][0]:
pop(start_q)
if start_q:
right_indices[end_q[0][1]] = start_q[0][1]
pop(end_q)
return right_indices
Leave a comment