-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathoverlapping_intervals.py
87 lines (62 loc) · 1.69 KB
/
overlapping_intervals.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
Given a collection of Intervals,merge all the overlapping Intervals.
For example:
Given [1,3], [2,6], [8,10], [15,18],
return [1,6], [8,10], [15,18].
Make sure the returned intervals are sorted.
Input:
The first line contains an integer T, depicting total number of test cases.
Then following T lines contains an integer N depicting the number of Intervals and next line followed by the intervals starting and ending positions 'x' and 'y' respectively.
Output:
Print the intervals after overlapping in sorted manner. There should be a newline at the end of output of every test case.
Constraints:
1 ≤ T ≤ 50
1 ≤ N ≤ 100
0 ≤ x ≤ y ≤ 100
Example:
Input
2
4
1 3 2 4 6 8 9 10
4
6 8 1 9 2 4 4 7
Output
1 4 6 8 9 10
1 9
"""
def merge_overlapping_interval(arr,n):
arr = sorted(arr)
prev = None
result = []
for item in arr:
if prev == None:
prev = item
continue
if item[0] <= prev[1]:
prev = (prev[0],max(prev[1],item[1]))
else:
result.append(prev)
prev = item
result.append(prev)
return result
def test():
n = 4
arr = [(1,1),(2,2),(6,8),(9,10)]
print(merge_overlapping_interval(arr,n))
arr = [(6,8),(1,9),(2,4),(4,7)]
print(merge_overlapping_interval(arr,n))
def print_interval(arr):
for item in arr:
print(item[0],item[1],end=" ")
print()
def main():
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int,input().split()))
arr = []
for j in range(n):
arr.append((a[2*j],a[2*j+1]))
print_interval(merge_overlapping_interval(arr,n))
if __name__ == '__main__':
main()