>>> start = [1, 3, 0, 5, 8, 5] >>> finish = [2, 4, 6, 7, 9, 9] >>> print_max_activities(start, finish) The following activities are selected: 0,1,3,4,
(start: list[int], finish: list[int])
| 9 | |
| 10 | |
| 11 | def print_max_activities(start: list[int], finish: list[int]) -> None: |
| 12 | """ |
| 13 | >>> start = [1, 3, 0, 5, 8, 5] |
| 14 | >>> finish = [2, 4, 6, 7, 9, 9] |
| 15 | >>> print_max_activities(start, finish) |
| 16 | The following activities are selected: |
| 17 | 0,1,3,4, |
| 18 | """ |
| 19 | n = len(finish) |
| 20 | print("The following activities are selected:") |
| 21 | |
| 22 | # The first activity is always selected |
| 23 | i = 0 |
| 24 | print(i, end=",") |
| 25 | |
| 26 | # Consider rest of the activities |
| 27 | for j in range(n): |
| 28 | # If this activity has start time greater than |
| 29 | # or equal to the finish time of previously |
| 30 | # selected activity, then select it |
| 31 | if start[j] >= finish[i]: |
| 32 | print(j, end=",") |
| 33 | i = j |
| 34 | |
| 35 | |
| 36 | if __name__ == "__main__": |