forked from Vishnu053/Daily-Coding-Problem-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200.py
31 lines (21 loc) · 727 Bytes
/
200.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
"""
Problem:
Let X be a set of n intervals on the real line. We say that a set of points P "stabs" X
if every interval in X contains at least one point in P. Compute the smallest set of
points that stabs X.
For example, given the intervals [(1, 4), (4, 5), (7, 9), (9, 12)], you should return
[4, 9].
"""
from typing import List, Tuple
def get_stab(list_of_intervals: List[Tuple[int]]) -> Tuple[int, int]:
start, end = zip(*list_of_intervals)
return min(end), max(start)
if __name__ == "__main__":
print(get_stab([(1, 4), (4, 5), (7, 9), (9, 12)]))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
[even though zip is a generator and takes O(1) space, destructuring the array takes
O(n) space]
"""