mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-22 11:10:14 +00:00
a936e94704
* Enable ruff ARG001 rule * Fix dynamic_programming/combination_sum_iv.py * Fix machine_learning/frequent_pattern_growth.py * Fix other/davis_putnam_logemann_loveland.py * Fix other/password.py * Fix * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix physics/n_body_simulation.py * Fix project_euler/problem_145/sol1.py * Fix project_euler/problem_174/sol1.py * Fix scheduling/highest_response_ratio_next.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix * Fix * Fix scheduling/job_sequencing_with_deadline.py * Fix scheduling/job_sequencing_with_deadline.py * Fix * Fix --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
def job_sequencing_with_deadlines(jobs: list) -> list:
|
|
"""
|
|
Function to find the maximum profit by doing jobs in a given time frame
|
|
|
|
Args:
|
|
jobs [list]: A list of tuples of (job_id, deadline, profit)
|
|
|
|
Returns:
|
|
max_profit [int]: Maximum profit that can be earned by doing jobs
|
|
in a given time frame
|
|
|
|
Examples:
|
|
>>> job_sequencing_with_deadlines(
|
|
... [(1, 4, 20), (2, 1, 10), (3, 1, 40), (4, 1, 30)])
|
|
[2, 60]
|
|
>>> job_sequencing_with_deadlines(
|
|
... [(1, 2, 100), (2, 1, 19), (3, 2, 27), (4, 1, 25), (5, 1, 15)])
|
|
[2, 127]
|
|
"""
|
|
|
|
# Sort the jobs in descending order of profit
|
|
jobs = sorted(jobs, key=lambda value: value[2], reverse=True)
|
|
|
|
# Create a list of size equal to the maximum deadline
|
|
# and initialize it with -1
|
|
max_deadline = max(jobs, key=lambda value: value[1])[1]
|
|
time_slots = [-1] * max_deadline
|
|
|
|
# Finding the maximum profit and the count of jobs
|
|
count = 0
|
|
max_profit = 0
|
|
for job in jobs:
|
|
# Find a free time slot for this job
|
|
# (Note that we start from the last possible slot)
|
|
for i in range(job[1] - 1, -1, -1):
|
|
if time_slots[i] == -1:
|
|
time_slots[i] = job[0]
|
|
count += 1
|
|
max_profit += job[2]
|
|
break
|
|
return [count, max_profit]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|