Very easy 8 line Python Code


  • 0
    S

    Easy to understand Python code below. Add newInterval to the existing intervals and keep merging.

            intervals = sorted(intervals+[newInterval], key = lambda x: x.start)
            output = [intervals[0]]
            for i in xrange(1, len(intervals)):
                if intervals[i].start <= output[-1].end:
                    output[-1] = Interval(output[-1].start, max(intervals[i].end, output[-1].end))
                else:
                    output.append(intervals[i])
            return output

Log in to reply
 

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