2020-05-22 06:10:11 +00:00
|
|
|
"""The following implementation assumes that the activities
|
2019-10-18 21:43:33 +00:00
|
|
|
are already sorted according to their finish time"""
|
2019-10-22 17:13:48 +00:00
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
"""Prints a maximum set of activities that can be done by a
|
2019-10-18 21:43:33 +00:00
|
|
|
single person, one at a time"""
|
2019-10-22 17:13:48 +00:00
|
|
|
# n --> Total number of activities
|
|
|
|
# start[]--> An array that contains start time of all activities
|
|
|
|
# finish[] --> An array that contains finish time of all activities
|
|
|
|
|
|
|
|
|
2022-10-29 20:45:21 +00:00
|
|
|
def print_max_activities(start: list[int], finish: list[int]) -> None:
|
2019-10-18 21:43:33 +00:00
|
|
|
"""
|
2020-05-22 06:10:11 +00:00
|
|
|
>>> start = [1, 3, 0, 5, 8, 5]
|
|
|
|
>>> finish = [2, 4, 6, 7, 9, 9]
|
2022-10-29 20:45:21 +00:00
|
|
|
>>> print_max_activities(start, finish)
|
2019-10-18 21:43:33 +00:00
|
|
|
The following activities are selected:
|
2020-09-10 08:31:26 +00:00
|
|
|
0,1,3,4,
|
2019-10-18 21:43:33 +00:00
|
|
|
"""
|
2019-10-22 17:13:48 +00:00
|
|
|
n = len(finish)
|
2019-10-18 21:43:33 +00:00
|
|
|
print("The following activities are selected:")
|
2019-10-22 17:13:48 +00:00
|
|
|
|
|
|
|
# The first activity is always selected
|
2019-10-18 21:43:33 +00:00
|
|
|
i = 0
|
2020-09-10 08:31:26 +00:00
|
|
|
print(i, end=",")
|
2019-10-22 17:13:48 +00:00
|
|
|
|
|
|
|
# Consider rest of the activities
|
|
|
|
for j in range(n):
|
|
|
|
# If this activity has start time greater than
|
|
|
|
# or equal to the finish time of previously
|
|
|
|
# selected activity, then select it
|
|
|
|
if start[j] >= finish[i]:
|
2020-09-10 08:31:26 +00:00
|
|
|
print(j, end=",")
|
2019-10-22 17:13:48 +00:00
|
|
|
i = j
|
|
|
|
|
|
|
|
|
2020-09-10 08:31:26 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
2019-10-18 21:43:33 +00:00
|
|
|
|
2020-09-10 08:31:26 +00:00
|
|
|
doctest.testmod()
|
|
|
|
|
|
|
|
start = [1, 3, 0, 5, 8, 5]
|
|
|
|
finish = [2, 4, 6, 7, 9, 9]
|
2022-10-29 20:45:21 +00:00
|
|
|
print_max_activities(start, finish)
|