One Pass Python Solution

  • 0
    def insert(self, intervals, newInterval):
            ret = []
            inserted = False
            for interval in intervals:
                if (not inserted) and interval.start > newInterval.start:
                    self.merge(ret, newInterval)
                    inserted = True
                self.merge(ret, interval)
            if not inserted:
                self.merge(ret, newInterval)
            return ret
    def merge(self, ret, interval):
            if ret and ret[-1].end >= interval.start:
                ret[-1].end = max(ret[-1].end, interval.end)

Log in to reply

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