Job Sequencing Problem | Set 3 (Using TreeSet in JAVA)
Given an array of jobs where every job has a deadline and associated profit (if the job is finished before the deadline). It is also given that every job takes a single unit of time, so the minimum possible deadline for any job is 1. How to maximize total profit if only one job can be scheduled at a time.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input : Four Jobs with following deadlines and profits JobID Deadline Profit a 4 20 b 1 10 c 1 40 d 1 30 Output : Following is maximum profit sequence of jobs c, a
Input : Five Jobs with following deadlines and profits JobID Deadline Profit a 2 100 b 1 19 c 2 27 d 1 25 e 3 15 Output : Following is maximum profit sequence of jobs c, a, e
Below is the step by step algorithm to solve the problem using TreeSet in Java:
- Sort all the jobs according to their respective profits in decreasing order.
- Create a TreeSet and insert all the integers from 0 to n-1.
- Traverse the array of jobs and for ith job
- Search for a time slot ‘x’ in the TreeSet with maximum value which is less than the deadline of the ith job.
- If any value exists then include that job in the answer and remove ‘x’ from the TreeSet
- Else check for the remaining jobs.
Below is the implementation of the above algorithm:
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)