Easy to Understand in Python, beating 88% (Optimized)


  • 0
    F

    Python is elegant, but slow at the same time.
    I spent a lot of time finding the bottlenecks, got a bunch of TLE in the process. Finally, well-optimized code beats 88%.

    class Solution(object):
        def findRightInterval(self, intervals):
            """
            :type intervals: List[Interval]
            :rtype: List[int]
            """
            import bisect
            s = {}
            for idx, interval in enumerate(intervals):
                s[interval.start] = idx
            ans = []
            key_sorted = sorted(list(s.keys()))
            for interval in intervals:
                idx = bisect.bisect_left(key_sorted, interval.end)
                ans.append(-1 if idx == len(s) else s[key_sorted[idx]])
            return ans
    ``

Log in to reply
 

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