-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsert Interval.py
49 lines (35 loc) · 1.63 KB
/
Insert Interval.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Link: https://leetcode.com/problems/insert-interval/submissions/
class Solution(object):
def insert(self, intervals, newInterval):
"""
:type intervals: List[List[int]]
:type newInterval: List[int]
:rtype: List[List[int]]
"""
# If intervals is empty, return newInterval
if len(intervals) == 0:
return [newInterval]
# Immediately add newInterval to intervals
intervals.append(newInterval)
# Sort list based on 1st element of each inner list
intervals.sort(key=lambda x: x[0])
# Create a new list to return
ans = list()
# Iterate intervals list and add non-overlapping elements to ans
for interval in intervals:
# If list is empty, add first element and skip to next iteration
if len(ans) == 0:
ans.append(interval)
continue
# Get recently added element from ans
lastInterval = ans[-1]
# If lastInterval and current interval doesn't overlap, merge both
if lastInterval[1] < interval[0]:
ans.append(interval)
else:
# Get smallest X-value and largest Y-value between both intervals
smallX = min(lastInterval[0], interval[0])
largeY = max(lastInterval[1], interval[1])
# Replace last interval with merged one
ans[-1] = [smallX, largeY]
return ans