Python reduction Solution


  • 0

    I'd like to do a reduction of this problem to simply merge all the intervals :)

    class Solution(object):
        def insert(self, intervals, newInterval):
            ans = []
            intervals.append(newInterval)
            intervals.sort(key = lambda x: x.start)
            for interval in intervals:
                if not ans:
                    ans.append(interval)
                elif interval.start <= ans[-1].end:
                    ans[-1].end = max(ans[-1].end, interval.end)
                else:
                    ans.append(interval)
            return ans
    

Log in to reply
 

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