-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn.py
More file actions
47 lines (29 loc) · 1.28 KB
/
Copy pathn.py
File metadata and controls
47 lines (29 loc) · 1.28 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
from __future__ import annotations
from collections.abc import Iterable
type Interval = tuple[int, int]
def merge_intervals(intervals: Iterable[Interval]) -> Iterable[Interval]:
intervals_list = list(intervals)
if not intervals_list:
return
intervals_list.sort()
previous_left_endpoint, previous_right_endpoint = intervals_list[0]
for i in range(1, len(intervals_list)):
left_endpoint, right_endpoint = intervals_list[i]
if left_endpoint <= previous_right_endpoint:
previous_right_endpoint = max(right_endpoint, previous_right_endpoint)
continue
yield previous_left_endpoint, previous_right_endpoint
previous_left_endpoint = left_endpoint
previous_right_endpoint = right_endpoint
yield previous_left_endpoint, previous_right_endpoint
def read_intervals(count: int) -> Iterable[Interval]:
for i in range(count):
left_endpoint, right_endpoint = map(int, input().strip().split())
yield left_endpoint, right_endpoint
def main() -> None:
intervals_count = int(input().strip())
intervals = read_intervals(intervals_count)
for left_endpoint, right_endpoint in merge_intervals(intervals):
print(left_endpoint, right_endpoint)
if __name__ == '__main__':
main()