436. Find Right Interval

https://leetcode.com/problems/find-right-interval/

import bisect
class Solution:
    def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
        res = []
        n = len(intervals)
        sortInterval = sorted([(x[0],i) for i,x in enumerate(intervals)], key = lambda x:x[0])
        starts,pos = [x[0] for x in sortInterval], [x[1] for x in sortInterval]
        for interval in intervals:
            end = interval[1]
            j = bisect.bisect_left(starts,end)
            if j == n:
                res.append(-1)
            else:
                res.append(pos[j])
        return res
                
        

Last updated

Was this helpful?