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
- TreeSet contains() Method in Java
- TreeSet last() Method in Java
- TreeSet first() Method in Java
- How to loop over TreeSet in Java
- TreeSet add() Method in Java
- HashSet vs TreeSet in Java
- TreeSet lower() method in Java
- TreeSet pollFirst() method in Java
- TreeSet subSet() Method in Java
- TreeSet tailSet() Method in Java
- TreeSet clear() Method in Java
- TreeSet clone() Method in Java
- TreeSet isEmpty() Method in Java
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.