-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeIntervals.py
More file actions
47 lines (40 loc) · 1.08 KB
/
MergeIntervals.py
File metadata and controls
47 lines (40 loc) · 1.08 KB
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
intervals = [[1,5]]
newInterval = [2,7]
#intervals.append(newInterval)
#intervals.sort()
l=len(intervals)
if not intervals and not newInterval:
print ([])
elif intervals and not newInterval:
res=[intervals[0]]
List=0
elif not intervals and newInterval:
res=[newInterval]
List=0
else:
if newInterval<intervals[0]:
res=[newInterval]
List=0
else:
res=[intervals[0]]
List=0
while List <l:
if res[-1]<newInterval and newInterval<intervals[List]:
if newInterval[0]>res[-1][-1]:
res.append(newInterval)
elif newInterval[-1]>res[-1][-1]:
res[-1][-1]=newInterval[-1]
continue
if intervals[List][0]<=res[-1][-1]:
if intervals[List][0]<=res[-1][0]:
res[-1][0]=intervals[List][0]
# else:
# res[-1][0]=res[-1][0]
if intervals[List][-1]>res[-1][-1]:
res[-1][-1]=intervals[List][-1]
# else:
# res[-1][-1]=intervals[List][-1]
else:
res.append(intervals[List])
List+=1
print (res)