From 51dba4d743cd2c8d407eea3e9cd4e7b2f69ee34d Mon Sep 17 00:00:00 2001 From: Lakshay Roopchandani <75477853+lakshayroop5@users.noreply.github.com> Date: Sun, 9 Oct 2022 18:23:44 +0530 Subject: [PATCH] Job sequencing with deadlines (#6854) * completed optimised code for job sequencing with deadline problem * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * completed optimised code for job sequencing with deadline problem * completed optimized code for job sequencing with deadline problem * completed optimised code for job sequencing with deadline problem * completed optimised code for job sequencing with deadline problem * completed optimised code for job sequencing with deadline problem * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * completed optimized code for the issue "Job Scheduling with deadlines" * completed optimized code for the issue "Job Scheduling with deadlines" * completed optimized code for the issue "Job Scheduling with deadlines" * Update greedy_methods/job_sequencing_with_deadline.py Co-authored-by: Christian Clauss * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Updated reviews * Updated reviews * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update and rename greedy_methods/job_sequencing_with_deadline.py to scheduling/job_sequencing_with_deadline.py Co-authored-by: lakshayroop5 <87693528+lavenroop5@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss --- scheduling/job_sequencing_with_deadline.py | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 scheduling/job_sequencing_with_deadline.py diff --git a/scheduling/job_sequencing_with_deadline.py b/scheduling/job_sequencing_with_deadline.py new file mode 100644 index 000000000..7b23c0b35 --- /dev/null +++ b/scheduling/job_sequencing_with_deadline.py @@ -0,0 +1,48 @@ +def job_sequencing_with_deadlines(num_jobs: int, jobs: list) -> list: + """ + Function to find the maximum profit by doing jobs in a given time frame + + Args: + num_jobs [int]: Number of jobs + 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(4, + ... [(1, 4, 20), (2, 1, 10), (3, 1, 40), (4, 1, 30)]) + [2, 60] + >>> job_sequencing_with_deadlines(5, + ... [(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()