class Solution:
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
"""
step1, check edge case if length is less than 2, if yes, return -1 based on length
step2, initialize res array filled with -1 and length is same as intervals
step3, use tuple (interval) as key, index as val to save original index for later sort
step4, sort by starting time
step5, loop through sorted interval, find the earliest interval that meet inter[i][1] <= inter[j][0],
if found, break current loop, find their index correspoindingly and update res array.
"""
# time O(N^2)for 2 loop space O(N) dictionary
if len(intervals) < 2:
return [-1]*len(intervals)
d = {}
res = [-1] * len(intervals)
for idx,inte in enumerate(intervals):
d[tuple(inte)] = idx # here using tuple because python doesn't accept list as key since list is unhashable
intervals.sort(key= lambda k: k[0])
for i in range(len(intervals)-1):
for j in range(i+1,len(intervals)):
if intervals[i][1]<=intervals[j][0]:
res[d[tuple(intervals[i])]] = d[tuple(intervals[j])]
break
return res
optimization: use binary search to speed up linear search. [TODO] bisect_left functhon and without help of built-in function