mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
c909da9b08
* pre-commit: Upgrade psf/black for stable style 2023 Updating https://github.com/psf/black ... updating 22.12.0 -> 23.1.0 for their `2023 stable style`. * https://github.com/psf/black/blob/main/CHANGES.md#2310 > This is the first [psf/black] release of 2023, and following our stability policy, it comes with a number of improvements to our stable style… Also, add https://github.com/tox-dev/pyproject-fmt and https://github.com/abravalheri/validate-pyproject to pre-commit. I only modified `.pre-commit-config.yaml` and all other files were modified by pre-commit.ci and psf/black. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
"""The following implementation assumes that the activities
|
|
are already sorted according to their finish time"""
|
|
|
|
"""Prints a maximum set of activities that can be done by a
|
|
single person, one at a time"""
|
|
# 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
|
|
|
|
|
|
def print_max_activities(start: list[int], finish: list[int]) -> None:
|
|
"""
|
|
>>> 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,
|
|
"""
|
|
n = len(finish)
|
|
print("The following activities are selected:")
|
|
|
|
# The first activity is always selected
|
|
i = 0
|
|
print(i, end=",")
|
|
|
|
# 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]:
|
|
print(j, end=",")
|
|
i = j
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|
|
|
|
start = [1, 3, 0, 5, 8, 5]
|
|
finish = [2, 4, 6, 7, 9, 9]
|
|
print_max_activities(start, finish)
|