-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert-interval.py
More file actions
35 lines (30 loc) · 916 Bytes
/
insert-interval.py
File metadata and controls
35 lines (30 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# encoding: utf-8
__author__ = 'zhangwei'
# Definition for an interval.
# class Interval:
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
class Solution:
# @param {Interval[]} intervals
# @param {Interval} newInterval
# @return {Interval[]}
def insert(self, intervals, newInterval):
n = len(intervals)
if n == 0:
return [newInterval]
starts = [interval.start for interval in intervals]
starts.append(newInterval.start)
ends = [interval.end for interval in intervals]
ends.append(newInterval.end)
starts.sort()
ends.sort()
results = []
i = 0
while i <= n:
j = i
while j < n and ends[j] >= starts[j+1]:
j += 1
results.append(Interval(s=starts[i], e=ends[j]))
i = j+1
return results