-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcovering_segments.py
More file actions
31 lines (25 loc) · 927 Bytes
/
covering_segments.py
File metadata and controls
31 lines (25 loc) · 927 Bytes
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
"""
Task:
Given a set of n segments {[a0,b0],[a1,b1]....[an-1,bn-1]} with integer
coordinates on a line, find the minimum number 'm' of points such that
each segment contains at least one point .That is, find a set of
integers X of the minimum size such that for any segment [ai,bi] there
is a point x belongs X such that ai <= x <= bi
>>> minimum_points([(1, 3), (2, 5), (3, 6)])
1
>>> minimum_points([(4, 7), (1, 3), (2, 5), (5, 6)])
2
"""
def minimum_points(segments):
points = []
segs = sorted(segments, key=lambda x: x[1])
for seg in segs:
# check whether this segements is covered by points
if points and any(seg[0] <= p <= seg[1] for p in points):
continue
# safe first move to add smallest end point or largest start point
points.append(seg[1])
return len(points)
if __name__ == '__main__':
import doctest
doctest.testmod()