Python Easy Solution


  • 0
    D
    # Definition for an interval.
    # class Interval(object):
    #     def __init__(self, s=0, e=0):
    #         self.start = s
    #         self.end = e
    
    class Solution(object):
        def findRightInterval(self, intervals):
            """
            :type intervals: List[Interval]
            :rtype: List[int]
            """
            m = {} # {start->index}
            for i in range(len(intervals)):
                m[intervals[i].start] = i
            starts = sorted(m.keys())
            ends = sorted(x.end for x in intervals)
            ans = {} # end -> start
            j = 0
            for x in ends:
                while j < len(starts) and starts[j] < x: j += 1
                ans[x] = starts[j] if j < len(starts) else None
            return [m[ans[x.end]] if ans[x.end] != None else -1 for x in intervals]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.