|
| 1 | +"""The following implementation assumes that the activities |
| 2 | +are already sorted according to their finish time""" |
| 3 | + |
| 4 | +"""Prints a maximum set of activities that can be done by a |
| 5 | +single person, one at a time""" |
| 6 | +# n --> Total number of activities |
| 7 | +# start[]--> An array that contains start time of all activities |
| 8 | +# finish[] --> An array that contains finish time of all activities |
| 9 | + |
| 10 | +def printMaxActivities(start, finish): |
| 11 | + """ |
| 12 | + >>> start = [1, 3, 0, 5, 8, 5] |
| 13 | + >>> finish = [2, 4, 6, 7, 9, 9] |
| 14 | + >>> printMaxActivities(start, finish) |
| 15 | + The following activities are selected: |
| 16 | + 0 1 3 4 |
| 17 | + """ |
| 18 | + n = len(finish) |
| 19 | + print("The following activities are selected:") |
| 20 | + |
| 21 | + # The first activity is always selected |
| 22 | + i = 0 |
| 23 | + print(i, end=" ") |
| 24 | + |
| 25 | + # Consider rest of the activities |
| 26 | + for j in range(n): |
| 27 | + |
| 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 | +# Driver program to test above function |
| 36 | +start = [1, 3, 0, 5, 8, 5] |
| 37 | +finish = [2, 4, 6, 7, 9, 9] |
| 38 | +printMaxActivities(start, finish) |
| 39 | + |
| 40 | +""" |
| 41 | +The following activities are selected: |
| 42 | +0 1 3 4 |
| 43 | +""" |
0 commit comments