VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximize-jobs-that-can-be-completed-under-given-constraint/

⇱ Maximize jobs that can be completed under given constraint - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximize jobs that can be completed under given constraint

Last Updated : 15 Jul, 2025

Given an integer N denoting number of jobs and a matrix ranges[] consisting of a range [start day, end day] for each job within which it needs to be completed, the task is to find the maximum possible jobs that can be completed.
Examples: 
 

Input: N = 5, Ranges = {{1, 5}, {1, 5}, {1, 5}, {2, 3}, {2, 3}} 
Output:
Explanation: Job 1 on day 1, Job 4 on day 2, Job 5 on day 3, Job 2 on day 4, Job 3 on day 5


Input: N=6, Ranges = {{1, 3}, {1, 3}, {2, 3}, {2, 3}, {1, 4}, {2, 5}} 
Output:
 


 


Approach: The above problem can be solved using a Priority Queue. Follow the steps below to solve the problems: 
 

  • Find the minimum and maximum day in the range of jobs.
  • Sort all jobs in increasing order of start day.
  • Iterate from the minimum to maximum day, and for every ith day, select the job having least end day which can be completed on that day.
  • In order to perform the above step, maintain a Min Heap, and on every ith day, insert the jobs that can be completed on that day, into the Min Heap sorted by end day. If any job can completed on the ith day, consider the one with the lowest end day and increase the count of jobs completed.
  • Repeat this process for all the days and finally print the count of jobs completed.


Below is an implementation of the above approach:
 


Output: 
5

 

Time Complexity: O(Xlog(N)), where X is the difference between maximum and minimum day and N is the number of jobs. 
Auxiliary Space: O(N2)
 

Comment