Skip to content

Commit 7f047c6

Browse files
committed
Fix
1 parent 271ddc5 commit 7f047c6

File tree

2 files changed

+58
-29
lines changed

2 files changed

+58
-29
lines changed

src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
/**
77
* A class that implements a job scheduling algorithm to maximize profit
8-
* while adhering to job deadlines.
8+
* while adhering to job deadlines and arrival times.
99
*
10-
* This class provides functionality to schedule jobs based on their profit
11-
* and deadlines to ensure that the maximum number of jobs is completed
10+
* This class provides functionality to schedule jobs based on their profit,
11+
* arrival time, and deadlines to ensure that the maximum number of jobs is completed
1212
* within the given timeframe. It sorts the jobs in decreasing order of profit
1313
* and attempts to assign them to the latest possible time slots.
1414
*/
@@ -17,39 +17,42 @@ private JobSchedulingWithDeadline() {
1717
}
1818

1919
/**
20-
* Represents a job with an ID, deadline, and profit.
20+
* Represents a job with an ID, arrival time, deadline, and profit.
2121
*
22-
* Each job has a unique identifier, a deadline by which it must be completed,
23-
* and a profit associated with completing the job.
22+
* Each job has a unique identifier, an arrival time (when it becomes available for scheduling),
23+
* a deadline by which it must be completed, and a profit associated with completing the job.
2424
*/
2525
static class Job {
2626
int jobId;
27+
int arrivalTime;
2728
int deadline;
2829
int profit;
2930

3031
/**
31-
* Constructs a Job instance with the specified job ID, deadline, and profit.
32+
* Constructs a Job instance with the specified job ID, arrival time, deadline, and profit.
3233
*
33-
* @param jobId Unique identifier for the job
34-
* @param deadline Deadline for completing the job
35-
* @param profit Profit earned upon completing the job
34+
* @param jobId Unique identifier for the job
35+
* @param arrivalTime Time when the job becomes available for scheduling
36+
* @param deadline Deadline for completing the job
37+
* @param profit Profit earned upon completing the job
3638
*/
37-
Job(int jobId, int deadline, int profit) {
39+
Job(int jobId, int arrivalTime, int deadline, int profit) {
3840
this.jobId = jobId;
41+
this.arrivalTime = arrivalTime;
3942
this.deadline = deadline;
4043
this.profit = profit;
4144
}
4245
}
4346

4447
/**
45-
* Schedules jobs to maximize profit while respecting their deadlines.
48+
* Schedules jobs to maximize profit while respecting their deadlines and arrival times.
4649
*
4750
* This method sorts the jobs in descending order of profit and attempts
48-
* to allocate them to time slots that are before or on their deadlines.
49-
* The function returns an array where the first element is the total number
50-
* of jobs scheduled and the second element is the total profit earned.
51+
* to allocate them to time slots that are before or on their deadlines,
52+
* provided they have arrived. The function returns an array where the first element
53+
* is the total number of jobs scheduled and the second element is the total profit earned.
5154
*
52-
* @param jobs An array of Job objects, each representing a job with an ID,
55+
* @param jobs An array of Job objects, each representing a job with an ID, arrival time,
5356
* deadline, and profit.
5457
* @return An array of two integers: the first element is the count of jobs
5558
* that were successfully scheduled, and the second element is the
@@ -68,12 +71,14 @@ public static int[] jobSequencingWithDeadlines(Job[] jobs) {
6871

6972
// Schedule the jobs
7073
for (Job job : jobs) {
71-
for (int i = job.deadline - 1; i >= 0; i--) {
72-
if (timeSlots[i] == -1) {
73-
timeSlots[i] = job.jobId;
74-
count++;
75-
maxProfit += job.profit;
76-
break;
74+
if (job.arrivalTime <= job.deadline) {
75+
for (int i = Math.min(job.deadline - 1, maxDeadline - 1); i >= job.arrivalTime - 1; i--) {
76+
if (timeSlots[i] == -1) {
77+
timeSlots[i] = job.jobId;
78+
count++;
79+
maxProfit += job.profit;
80+
break;
81+
}
7782
}
7883
}
7984
}

src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,53 @@ class JobSchedulingWithDeadlineTest {
88

99
@Test
1010
void testJobSequencingWithDeadlines1() {
11-
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)};
11+
JobSchedulingWithDeadline.Job[] jobs = {
12+
new JobSchedulingWithDeadline.Job(1, 1, 4, 20),
13+
new JobSchedulingWithDeadline.Job(2, 1, 1, 10),
14+
new JobSchedulingWithDeadline.Job(3, 1, 1, 40),
15+
new JobSchedulingWithDeadline.Job(4, 1, 1, 30)
16+
};
1217
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs);
13-
assertArrayEquals(new int[] {2, 60}, result);
18+
assertArrayEquals(new int[] {2, 60}, result); // Expected output: 2 jobs, 60 profit
1419
}
1520

1621
@Test
1722
void testJobSequencingWithDeadlines2() {
18-
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)};
23+
JobSchedulingWithDeadline.Job[] jobs = {
24+
new JobSchedulingWithDeadline.Job(1, 1, 2, 100),
25+
new JobSchedulingWithDeadline.Job(2, 1, 1, 19),
26+
new JobSchedulingWithDeadline.Job(3, 1, 2, 27),
27+
new JobSchedulingWithDeadline.Job(4, 1, 1, 25),
28+
new JobSchedulingWithDeadline.Job(5, 1, 1, 15)
29+
};
1930
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs);
20-
assertArrayEquals(new int[] {2, 127}, result);
31+
assertArrayEquals(new int[] {2, 127}, result); // Expected output: 2 jobs, 127 profit
32+
}
33+
34+
@Test
35+
void testJobSequencingWithDeadlinesWithArrivalTimes() {
36+
JobSchedulingWithDeadline.Job[] jobs = {
37+
new JobSchedulingWithDeadline.Job(1, 2, 5, 50),
38+
new JobSchedulingWithDeadline.Job(2, 3, 4, 60),
39+
new JobSchedulingWithDeadline.Job(3, 1, 3, 20)
40+
};
41+
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs);
42+
assertArrayEquals(new int[] {3, 130}, result); // All 3 jobs fit within their deadlines
2143
}
2244

2345
@Test
2446
void testJobSequencingWithDeadlinesNoJobs() {
2547
JobSchedulingWithDeadline.Job[] jobs = {};
2648
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs);
27-
assertArrayEquals(new int[] {0, 0}, result);
49+
assertArrayEquals(new int[] {0, 0}, result); // No jobs, 0 profit
2850
}
2951

3052
@Test
3153
void testJobSequencingWithDeadlinesSingleJob() {
32-
JobSchedulingWithDeadline.Job[] jobs = {new JobSchedulingWithDeadline.Job(1, 1, 50)};
54+
JobSchedulingWithDeadline.Job[] jobs = {
55+
new JobSchedulingWithDeadline.Job(1, 1, 1, 50)
56+
};
3357
int[] result = JobSchedulingWithDeadline.jobSequencingWithDeadlines(jobs);
34-
assertArrayEquals(new int[] {1, 50}, result);
58+
assertArrayEquals(new int[] {1, 50}, result); // 1 job scheduled, 50 profit
3559
}
3660
}

0 commit comments

Comments
 (0)