7-line Python solution, same with problem 56, not efficient but very straightforward


  • 0
    K

    Sort all starts and ends. key = lambda x:(x[0], -x[1]) ensures (5, -1) will be in front of (5, 1). Then check overlapping.

    def insert(self, A, newI):
        result, overlap, begin = [], 0, float('-inf')
        for x, y in sorted([(i.start, 1) for i in A] + [(i.end, -1) for i in A] + 
                [(newI.start, 1), (newI.end, -1)], key = lambda x:(x[0], -x[1])):
            if y == 1 and not overlap: begin = x
            if y == -1 and overlap == 1: result += Interval(begin, x),
            overlap += y
        return result

Log in to reply
 

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