|
| 1 | +package com.thealgorithms.scheduling; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Comparator; |
| 5 | + |
| 6 | +/** |
| 7 | + * A class that implements a job scheduling algorithm to maximize profit |
| 8 | + * while adhering to job deadlines and arrival times. |
| 9 | + * |
| 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 |
| 12 | + * within the given timeframe. It sorts the jobs in decreasing order of profit |
| 13 | + * and attempts to assign them to the latest possible time slots. |
| 14 | + */ |
| 15 | +public final class JobSchedulingWithDeadline { |
| 16 | + private JobSchedulingWithDeadline() { |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Represents a job with an ID, arrival time, deadline, and profit. |
| 21 | + * |
| 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. |
| 24 | + */ |
| 25 | + static class Job { |
| 26 | + int jobId; |
| 27 | + int arrivalTime; |
| 28 | + int deadline; |
| 29 | + int profit; |
| 30 | + |
| 31 | + /** |
| 32 | + * Constructs a Job instance with the specified job ID, arrival time, deadline, and profit. |
| 33 | + * |
| 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 |
| 38 | + */ |
| 39 | + Job(int jobId, int arrivalTime, int deadline, int profit) { |
| 40 | + this.jobId = jobId; |
| 41 | + this.arrivalTime = arrivalTime; |
| 42 | + this.deadline = deadline; |
| 43 | + this.profit = profit; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Schedules jobs to maximize profit while respecting their deadlines and arrival times. |
| 49 | + * |
| 50 | + * This method sorts the jobs in descending order of profit and attempts |
| 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. |
| 54 | + * |
| 55 | + * @param jobs An array of Job objects, each representing a job with an ID, arrival time, |
| 56 | + * deadline, and profit. |
| 57 | + * @return An array of two integers: the first element is the count of jobs |
| 58 | + * that were successfully scheduled, and the second element is the |
| 59 | + * total profit earned from those jobs. |
| 60 | + */ |
| 61 | + public static int[] jobSequencingWithDeadlines(Job[] jobs) { |
| 62 | + Arrays.sort(jobs, Comparator.comparingInt(job -> - job.profit)); |
| 63 | + |
| 64 | + int maxDeadline = Arrays.stream(jobs).mapToInt(job -> job.deadline).max().orElse(0); |
| 65 | + |
| 66 | + int[] timeSlots = new int[maxDeadline]; |
| 67 | + Arrays.fill(timeSlots, -1); |
| 68 | + |
| 69 | + int count = 0; |
| 70 | + int maxProfit = 0; |
| 71 | + |
| 72 | + // Schedule the jobs |
| 73 | + for (Job job : jobs) { |
| 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 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return new int[] {count, maxProfit}; |
| 87 | + } |
| 88 | +} |
0 commit comments