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.
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)
- TreeSet in Java
- TreeSet in Java
- How to loop over TreeSet in Java
- TreeSet add() Method in Java
- TreeSet first() Method in Java
- HashSet vs TreeSet in Java
- TreeSet last() Method in Java
- TreeSet contains() Method in Java
- TreeSet iterator() Method in Java
- TreeSet size() Method in Java
- TreeSet remove() Method in Java
- TreeSet retainAll() method in Java with Example
- TreeSet clear() Method in Java
- TreeSet toArray() method in Java with Example
- TreeSet toArray(T) method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.