In this article we will discuss the Job sequencing problem in C#, we will achieve it using a greedy approach algorithm.
Problem statement
The job sequencing problem is a common problem that contains assigning a set of jobs to a set of machines, where each job has a deadline and a profit, and we have to get the maximum profit before the job deadline.
Given an array of jobs where each job has its deadline and profit. And Profit can get only if the job is finished before the job deadline also every job takes a single unit of time, so the minimum possible deadline for any job is 1 so we have to get the maximum profit before the job deadline.
For example,
JobID | Deadline | Profit |
1 | 3 | 90 |
2 | 1 | 20 |
3 | 2 | 30 |
4 | 1 | 25 |
5 | 2 | 15 |
Output
JobID:-> 4,3,1
C# code
Problem Explanation
This implementation is done by the greedy approach; we start by sorting the jobs in the decreasing order of the profit and then we determine the maximum deadline of the job then we initialize an array of slots with a length equal to the maximum deadline.
And then we iterate through each job and find the latest available slot for that job by starting from its deadline and going backward until we find an empty slot. If we find an available slot, we assign the job to that slot. If we don't find an available slot, we skip the job.
Finally, we print the selected jobs by iterating through the slots array and printing the job ID for each non-zero slot.
------------------------------------------