From 10f7e24d1991ffc40dc89cd6d7289eeb97f750d7 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 13:00:09 +0530 Subject: [PATCH 01/10] Add `JobSchedulingWithDeadline.java` new algorithm with Junit tests --- .../scheduling/JobSchedulingWithDeadline.java | 84 +++++++++++++++++++ .../JobSchedulingWithDeadlineTest.java | 49 +++++++++++ 2 files changed, 133 insertions(+) create mode 100644 src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java create mode 100644 src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java diff --git a/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java b/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java new file mode 100644 index 000000000000..7c86350f0292 --- /dev/null +++ b/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java @@ -0,0 +1,84 @@ +package com.thealgorithms.scheduling; + +import java.util.Arrays; +import java.util.Comparator; + +/** + * A class that implements a job scheduling algorithm to maximize profit + * while adhering to job deadlines. + * + * This class provides functionality to schedule jobs based on their profit + * and deadlines to ensure that the maximum number of jobs is completed + * within the given timeframe. It sorts the jobs in decreasing order of profit + * and attempts to assign them to the latest possible time slots. + */ +public class JobSchedulingWithDeadline { + + /** + * Represents a job with an ID, deadline, and profit. + * + * Each job has a unique identifier, a deadline by which it must be completed, + * and a profit associated with completing the job. + */ + static class Job { + int jobId; + int deadline; + int profit; + + /** + * Constructs a Job instance with the specified job ID, deadline, and profit. + * + * @param jobId Unique identifier for the job + * @param deadline Deadline for completing the job + * @param profit Profit earned upon completing the job + */ + Job(int jobId, int deadline, int profit) { + this.jobId = jobId; + this.deadline = deadline; + this.profit = profit; + } + } + + /** + * Schedules jobs to maximize profit while respecting their deadlines. + * + * This method sorts the jobs in descending order of profit and attempts + * to allocate them to time slots that are before or on their deadlines. + * The function returns an array where the first element is the total number + * of jobs scheduled and the second element is the total profit earned. + * + * @param jobs An array of Job objects, each representing a job with an ID, + * deadline, and profit. + * @return An array of two integers: the first element is the count of jobs + * that were successfully scheduled, and the second element is the + * total profit earned from those jobs. + */ + public static int[] jobSequencingWithDeadlines(Job[] jobs) { + Arrays.sort(jobs, Comparator.comparingInt(job -> -job.profit)); + + int maxDeadline = Arrays.stream(jobs) + .mapToInt(job -> job.deadline) + .max() + .orElse(0); + + int[] timeSlots = new int[maxDeadline]; + Arrays.fill(timeSlots, -1); + + int count = 0; + int maxProfit = 0; + + // Schedule the jobs + for (Job job : jobs) { + for (int i = job.deadline - 1; i >= 0; i--) { + if (timeSlots[i] == -1) { + timeSlots[i] = job.jobId; + count++; + maxProfit += job.profit; + break; + } + } + } + + return new int[] {count, maxProfit}; + } +} diff --git a/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java b/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java new file mode 100644 index 000000000000..02fc1f410a32 --- /dev/null +++ b/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java @@ -0,0 +1,49 @@ +package com.thealgorithms.scheduling; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import org.junit.jupiter.api.Test; + +class JobSchedulingWithDeadlineTest { + + @Test + void testJobSequencingWithDeadlines1() { + JobSchedulingWithDeadline.Job[] jobs = { + new JobSchedulingWithDeadline.Job(1, 4, 20), + new JobSchedulingWithDeadline.Job(2, 1, 10), + new JobSchedulingWithDeadline.Job(3, 1, 40), + new JobSchedulingWithDeadline.Job(4, 1, 30) + }; + int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); + assertArrayEquals(new int[]{2, 60}, result); + } + + @Test + void testJobSequencingWithDeadlines2() { + JobSchedulingWithDeadline.Job[] jobs = { + new JobSchedulingWithDeadline.Job(1, 2, 100), + new JobSchedulingWithDeadline.Job(2, 1, 19), + new JobSchedulingWithDeadline.Job(3, 2, 27), + new JobSchedulingWithDeadline.Job(4, 1, 25), + new JobSchedulingWithDeadline.Job(5, 1, 15) + }; + int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); + assertArrayEquals(new int[]{2, 127}, result); + } + + @Test + void testJobSequencingWithDeadlinesNoJobs() { + JobSchedulingWithDeadline.Job[] jobs = {}; + int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); + assertArrayEquals(new int[]{0, 0}, result); + } + + @Test + void testJobSequencingWithDeadlinesSingleJob() { + JobSchedulingWithDeadline.Job[] jobs = { + new JobSchedulingWithDeadline.Job(1, 1, 50) + }; + int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); + assertArrayEquals(new int[]{1, 50}, result); + } +} From 2a0e7f17e86c79941befda549934b941736fc281 Mon Sep 17 00:00:00 2001 From: Hardvan Date: Sun, 6 Oct 2024 07:30:26 +0000 Subject: [PATCH 02/10] Update directory --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1bad5d3b98a3..5d4203fbfd6f 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -457,6 +457,7 @@ * [GenerateSubsets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java) * scheduling * [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java) + * [JobSchedulingWithDeadline](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java) * [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java) * [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java) * [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java) @@ -911,6 +912,7 @@ * [GenerateSubsetsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java) * scheduling * [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java) + * [JobSchedulingWithDeadlineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java) * [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java) * [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java) * [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java) From 66b7ddb18b6be4a830185dee5a699009d6602b8c Mon Sep 17 00:00:00 2001 From: Hardvan Date: Sun, 6 Oct 2024 07:33:49 +0000 Subject: [PATCH 03/10] Update directory --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index d250ab6524ec..18e145a62dfa 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -460,6 +460,7 @@ * scheduling * [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java) * [JobSchedulingWithDeadline](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java) + * [MLFQScheduler](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MLFQScheduler.java) * [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java) * [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java) * [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java) @@ -917,6 +918,7 @@ * scheduling * [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java) * [JobSchedulingWithDeadlineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java) + * [MLFQSchedulerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MLFQSchedulerTest.java) * [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java) * [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java) * [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java) From d68bbd5013ccdf109a6e3cb6e7b9ae8a067b9b4f Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 13:05:38 +0530 Subject: [PATCH 04/10] Fix --- .../scheduling/JobSchedulingWithDeadline.java | 7 ++--- .../JobSchedulingWithDeadlineTest.java | 27 +++++-------------- 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java b/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java index 7c86350f0292..c064aa53619c 100644 --- a/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java +++ b/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java @@ -54,12 +54,9 @@ static class Job { * total profit earned from those jobs. */ public static int[] jobSequencingWithDeadlines(Job[] jobs) { - Arrays.sort(jobs, Comparator.comparingInt(job -> -job.profit)); + Arrays.sort(jobs, Comparator.comparingInt(job -> - job.profit)); - int maxDeadline = Arrays.stream(jobs) - .mapToInt(job -> job.deadline) - .max() - .orElse(0); + int maxDeadline = Arrays.stream(jobs).mapToInt(job -> job.deadline).max().orElse(0); int[] timeSlots = new int[maxDeadline]; Arrays.fill(timeSlots, -1); diff --git a/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java b/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java index 02fc1f410a32..ff42d23f9178 100644 --- a/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java +++ b/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java @@ -8,42 +8,29 @@ class JobSchedulingWithDeadlineTest { @Test void testJobSequencingWithDeadlines1() { - JobSchedulingWithDeadline.Job[] jobs = { - new JobSchedulingWithDeadline.Job(1, 4, 20), - new JobSchedulingWithDeadline.Job(2, 1, 10), - new JobSchedulingWithDeadline.Job(3, 1, 40), - new JobSchedulingWithDeadline.Job(4, 1, 30) - }; + JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 4, 20), new JobSchedulingWithDeadline.Job(2, 1, 10), new JobSchedulingWithDeadline.Job(3, 1, 40), new JobSchedulingWithDeadline.Job(4, 1, 30)}; int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); - assertArrayEquals(new int[]{2, 60}, result); + assertArrayEquals(new int[] {2, 60}, result); } @Test void testJobSequencingWithDeadlines2() { - JobSchedulingWithDeadline.Job[] jobs = { - new JobSchedulingWithDeadline.Job(1, 2, 100), - new JobSchedulingWithDeadline.Job(2, 1, 19), - new JobSchedulingWithDeadline.Job(3, 2, 27), - new JobSchedulingWithDeadline.Job(4, 1, 25), - new JobSchedulingWithDeadline.Job(5, 1, 15) - }; + JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 2, 100), new JobSchedulingWithDeadline.Job(2, 1, 19), new JobSchedulingWithDeadline.Job(3, 2, 27), new JobSchedulingWithDeadline.Job(4, 1, 25), new JobSchedulingWithDeadline.Job(5, 1, 15)}; int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); - assertArrayEquals(new int[]{2, 127}, result); + assertArrayEquals(new int[] {2, 127}, result); } @Test void testJobSequencingWithDeadlinesNoJobs() { JobSchedulingWithDeadline.Job[] jobs = {}; int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); - assertArrayEquals(new int[]{0, 0}, result); + assertArrayEquals(new int[] {0, 0}, result); } @Test void testJobSequencingWithDeadlinesSingleJob() { - JobSchedulingWithDeadline.Job[] jobs = { - new JobSchedulingWithDeadline.Job(1, 1, 50) - }; + JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 1, 50)}; int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); - assertArrayEquals(new int[]{1, 50}, result); + assertArrayEquals(new int[] {1, 50}, result); } } From 797623a4f7fa7affe530b2a6a76cb2cea59245f4 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 13:07:26 +0530 Subject: [PATCH 05/10] Fix --- .../com/thealgorithms/scheduling/JobSchedulingWithDeadline.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java b/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java index c064aa53619c..a7d0433c31dd 100644 --- a/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java +++ b/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java @@ -13,6 +13,8 @@ * and attempts to assign them to the latest possible time slots. */ public class JobSchedulingWithDeadline { + private JobSchedulingWithDeadline() { + } /** * Represents a job with an ID, deadline, and profit. From 271ddc5cacbc2db3c0d3ede7a51cf59471abf807 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 13:09:02 +0530 Subject: [PATCH 06/10] Fix --- .../com/thealgorithms/scheduling/JobSchedulingWithDeadline.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java b/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java index a7d0433c31dd..26c775c4f58d 100644 --- a/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java +++ b/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java @@ -12,7 +12,7 @@ * within the given timeframe. It sorts the jobs in decreasing order of profit * and attempts to assign them to the latest possible time slots. */ -public class JobSchedulingWithDeadline { +public final class JobSchedulingWithDeadline { private JobSchedulingWithDeadline() { } From bffda74e096caedeb814fd3f9dba2f8decf60d4a Mon Sep 17 00:00:00 2001 From: siriak Date: Mon, 7 Oct 2024 15:11:09 +0000 Subject: [PATCH 07/10] Update directory --- DIRECTORY.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9678e437e4d1..ffbf6534a3c3 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -467,8 +467,8 @@ * [GenerateSubsets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java) * scheduling * [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java) - * [JobSchedulingWithDeadline](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java) * [HighestResponseRatioNextScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java) + * [JobSchedulingWithDeadline](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java) * [MLFQScheduler](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MLFQScheduler.java) * [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java) * [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java) @@ -941,8 +941,8 @@ * [GenerateSubsetsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java) * scheduling * [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java) - * [JobSchedulingWithDeadlineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java) * [HighestResponseRatioNextSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java) + * [JobSchedulingWithDeadlineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java) * [MLFQSchedulerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MLFQSchedulerTest.java) * [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java) * [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java) From 7f047c6eedd72f4d095317f0376955c3d88482f7 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 8 Oct 2024 15:22:27 +0530 Subject: [PATCH 08/10] Fix --- .../scheduling/JobSchedulingWithDeadline.java | 49 ++++++++++--------- .../JobSchedulingWithDeadlineTest.java | 38 +++++++++++--- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java b/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java index 26c775c4f58d..49638d39fc2a 100644 --- a/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java +++ b/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java @@ -5,10 +5,10 @@ /** * A class that implements a job scheduling algorithm to maximize profit - * while adhering to job deadlines. + * while adhering to job deadlines and arrival times. * - * This class provides functionality to schedule jobs based on their profit - * and deadlines to ensure that the maximum number of jobs is completed + * This class provides functionality to schedule jobs based on their profit, + * arrival time, and deadlines to ensure that the maximum number of jobs is completed * within the given timeframe. It sorts the jobs in decreasing order of profit * and attempts to assign them to the latest possible time slots. */ @@ -17,39 +17,42 @@ private JobSchedulingWithDeadline() { } /** - * Represents a job with an ID, deadline, and profit. + * Represents a job with an ID, arrival time, deadline, and profit. * - * Each job has a unique identifier, a deadline by which it must be completed, - * and a profit associated with completing the job. + * Each job has a unique identifier, an arrival time (when it becomes available for scheduling), + * a deadline by which it must be completed, and a profit associated with completing the job. */ static class Job { int jobId; + int arrivalTime; int deadline; int profit; /** - * Constructs a Job instance with the specified job ID, deadline, and profit. + * Constructs a Job instance with the specified job ID, arrival time, deadline, and profit. * - * @param jobId Unique identifier for the job - * @param deadline Deadline for completing the job - * @param profit Profit earned upon completing the job + * @param jobId Unique identifier for the job + * @param arrivalTime Time when the job becomes available for scheduling + * @param deadline Deadline for completing the job + * @param profit Profit earned upon completing the job */ - Job(int jobId, int deadline, int profit) { + Job(int jobId, int arrivalTime, int deadline, int profit) { this.jobId = jobId; + this.arrivalTime = arrivalTime; this.deadline = deadline; this.profit = profit; } } /** - * Schedules jobs to maximize profit while respecting their deadlines. + * Schedules jobs to maximize profit while respecting their deadlines and arrival times. * * This method sorts the jobs in descending order of profit and attempts - * to allocate them to time slots that are before or on their deadlines. - * The function returns an array where the first element is the total number - * of jobs scheduled and the second element is the total profit earned. + * to allocate them to time slots that are before or on their deadlines, + * provided they have arrived. The function returns an array where the first element + * is the total number of jobs scheduled and the second element is the total profit earned. * - * @param jobs An array of Job objects, each representing a job with an ID, + * @param jobs An array of Job objects, each representing a job with an ID, arrival time, * deadline, and profit. * @return An array of two integers: the first element is the count of jobs * that were successfully scheduled, and the second element is the @@ -68,12 +71,14 @@ public static int[] jobSequencingWithDeadlines(Job[] jobs) { // Schedule the jobs for (Job job : jobs) { - for (int i = job.deadline - 1; i >= 0; i--) { - if (timeSlots[i] == -1) { - timeSlots[i] = job.jobId; - count++; - maxProfit += job.profit; - break; + if (job.arrivalTime <= job.deadline) { + for (int i = Math.min(job.deadline - 1, maxDeadline - 1); i >= job.arrivalTime - 1; i--) { + if (timeSlots[i] == -1) { + timeSlots[i] = job.jobId; + count++; + maxProfit += job.profit; + break; + } } } } diff --git a/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java b/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java index ff42d23f9178..07251867ad23 100644 --- a/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java +++ b/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java @@ -8,29 +8,53 @@ class JobSchedulingWithDeadlineTest { @Test void testJobSequencingWithDeadlines1() { - JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 4, 20), new JobSchedulingWithDeadline.Job(2, 1, 10), new JobSchedulingWithDeadline.Job(3, 1, 40), new JobSchedulingWithDeadline.Job(4, 1, 30)}; + JobSchedulingWithDeadline.Job[] jobs = { + new JobSchedulingWithDeadline.Job(1, 1, 4, 20), + new JobSchedulingWithDeadline.Job(2, 1, 1, 10), + new JobSchedulingWithDeadline.Job(3, 1, 1, 40), + new JobSchedulingWithDeadline.Job(4, 1, 1, 30) + }; int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); - assertArrayEquals(new int[] {2, 60}, result); + assertArrayEquals(new int[] {2, 60}, result); // Expected output: 2 jobs, 60 profit } @Test void testJobSequencingWithDeadlines2() { - JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 2, 100), new JobSchedulingWithDeadline.Job(2, 1, 19), new JobSchedulingWithDeadline.Job(3, 2, 27), new JobSchedulingWithDeadline.Job(4, 1, 25), new JobSchedulingWithDeadline.Job(5, 1, 15)}; + JobSchedulingWithDeadline.Job[] jobs = { + new JobSchedulingWithDeadline.Job(1, 1, 2, 100), + new JobSchedulingWithDeadline.Job(2, 1, 1, 19), + new JobSchedulingWithDeadline.Job(3, 1, 2, 27), + new JobSchedulingWithDeadline.Job(4, 1, 1, 25), + new JobSchedulingWithDeadline.Job(5, 1, 1, 15) + }; int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); - assertArrayEquals(new int[] {2, 127}, result); + assertArrayEquals(new int[] {2, 127}, result); // Expected output: 2 jobs, 127 profit + } + + @Test + void testJobSequencingWithDeadlinesWithArrivalTimes() { + JobSchedulingWithDeadline.Job[] jobs = { + new JobSchedulingWithDeadline.Job(1, 2, 5, 50), + new JobSchedulingWithDeadline.Job(2, 3, 4, 60), + new JobSchedulingWithDeadline.Job(3, 1, 3, 20) + }; + int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); + assertArrayEquals(new int[] {3, 130}, result); // All 3 jobs fit within their deadlines } @Test void testJobSequencingWithDeadlinesNoJobs() { JobSchedulingWithDeadline.Job[] jobs = {}; int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); - assertArrayEquals(new int[] {0, 0}, result); + assertArrayEquals(new int[] {0, 0}, result); // No jobs, 0 profit } @Test void testJobSequencingWithDeadlinesSingleJob() { - JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 1, 50)}; + JobSchedulingWithDeadline.Job[] jobs = { + new JobSchedulingWithDeadline.Job(1, 1, 1, 50) + }; int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); - assertArrayEquals(new int[] {1, 50}, result); + assertArrayEquals(new int[] {1, 50}, result); // 1 job scheduled, 50 profit } } From 53d50df32f3cefc90ae6e7374f19e11e09c338db Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 8 Oct 2024 15:25:45 +0530 Subject: [PATCH 09/10] Fix --- .../JobSchedulingWithDeadlineTest.java | 21 +++---------------- 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java b/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java index 07251867ad23..fe94312298ad 100644 --- a/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java +++ b/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java @@ -8,36 +8,21 @@ class JobSchedulingWithDeadlineTest { @Test void testJobSequencingWithDeadlines1() { - JobSchedulingWithDeadline.Job[] jobs = { - new JobSchedulingWithDeadline.Job(1, 1, 4, 20), - new JobSchedulingWithDeadline.Job(2, 1, 1, 10), - new JobSchedulingWithDeadline.Job(3, 1, 1, 40), - new JobSchedulingWithDeadline.Job(4, 1, 1, 30) - }; + JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 1, 4, 20), new JobSchedulingWithDeadline.Job(2, 1, 1, 10), new JobSchedulingWithDeadline.Job(3, 1, 1, 40), new JobSchedulingWithDeadline.Job(4, 1, 1, 30)}; int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); assertArrayEquals(new int[] {2, 60}, result); // Expected output: 2 jobs, 60 profit } @Test void testJobSequencingWithDeadlines2() { - JobSchedulingWithDeadline.Job[] jobs = { - new JobSchedulingWithDeadline.Job(1, 1, 2, 100), - new JobSchedulingWithDeadline.Job(2, 1, 1, 19), - new JobSchedulingWithDeadline.Job(3, 1, 2, 27), - new JobSchedulingWithDeadline.Job(4, 1, 1, 25), - new JobSchedulingWithDeadline.Job(5, 1, 1, 15) - }; + JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 1, 2, 100), new JobSchedulingWithDeadline.Job(2, 1, 1, 19), new JobSchedulingWithDeadline.Job(3, 1, 2, 27), new JobSchedulingWithDeadline.Job(4, 1, 1, 25), new JobSchedulingWithDeadline.Job(5, 1, 1, 15)}; int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); assertArrayEquals(new int[] {2, 127}, result); // Expected output: 2 jobs, 127 profit } @Test void testJobSequencingWithDeadlinesWithArrivalTimes() { - JobSchedulingWithDeadline.Job[] jobs = { - new JobSchedulingWithDeadline.Job(1, 2, 5, 50), - new JobSchedulingWithDeadline.Job(2, 3, 4, 60), - new JobSchedulingWithDeadline.Job(3, 1, 3, 20) - }; + JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 2, 5, 50), new JobSchedulingWithDeadline.Job(2, 3, 4, 60), new JobSchedulingWithDeadline.Job(3, 1, 3, 20)}; int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); assertArrayEquals(new int[] {3, 130}, result); // All 3 jobs fit within their deadlines } From e0126d87c9e7f3ee94bc3b660e89f7e7f99fc652 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 8 Oct 2024 15:26:36 +0530 Subject: [PATCH 10/10] Fix --- .../scheduling/JobSchedulingWithDeadlineTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java b/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java index fe94312298ad..538db92a1f26 100644 --- a/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java +++ b/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java @@ -36,9 +36,7 @@ void testJobSequencingWithDeadlinesNoJobs() { @Test void testJobSequencingWithDeadlinesSingleJob() { - JobSchedulingWithDeadline.Job[] jobs = { - new JobSchedulingWithDeadline.Job(1, 1, 1, 50) - }; + JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 1, 1, 50)}; int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs); assertArrayEquals(new int[] {1, 50}, result); // 1 job scheduled, 50 profit }