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:5

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:5

**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 i
^{th}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 i^{th}day, insert the jobs that can be completed on that day, into the**Min Heap***sorted by*. If any job can completed on the i**end day**^{th}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:

## C++

`// C++ Program to implement the ` `// above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find maxiumum ` `// number of jobs ` `int` `find_maximum_jobs( ` ` ` `int` `N, ` ` ` `vector<pair<` `int` `, ` `int` `> > ranges) ` `{ ` ` ` `// Min Heap ` ` ` `priority_queue<` `int` `, vector<` `int` `>, ` ` ` `greater<` `int` `> > ` ` ` `queue; ` ` ` ` ` `// Sort ranges by start day ` ` ` `sort(ranges.begin(), ranges.end()); ` ` ` ` ` `// Stores the minimum and maximum ` ` ` `// day in the ranges ` ` ` `int` `min_day = ranges[0].first; ` ` ` `int` `max_day = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `max_day ` ` ` `= max(max_day, ` ` ` `ranges[i].second); ` ` ` ` ` `int` `index = 0, count_jobs = 0; ` ` ` ` ` `// Iterating from min_day to max_day ` ` ` `for` `(` `int` `i = min_day; i <= max_day; i++) { ` ` ` ` ` `// Insert the end day of the jobs ` ` ` `// which can be completed on ` ` ` `// i-th day in a priority queue ` ` ` `while` `(index < ranges.size() ` ` ` `&& ranges[index].first <= i) { ` ` ` ` ` `queue.push(ranges[index].second); ` ` ` `index++; ` ` ` `} ` ` ` ` ` `// Pop all jobs whose end day ` ` ` `// is less than current day ` ` ` `while` `(!queue.empty() ` ` ` `&& queue.top() < i) ` ` ` `queue.pop(); ` ` ` ` ` `// If queue is empty, no job ` ` ` `// can be completed on ` ` ` `// the i-th day ` ` ` `if` `(queue.empty()) ` ` ` `continue` `; ` ` ` ` ` `// Increment the count of ` ` ` `// jobs completed ` ` ` `count_jobs++; ` ` ` ` ` `// Pop the job with ` ` ` `// least end day ` ` ` `queue.pop(); ` ` ` `} ` ` ` ` ` `// Return the jobs ` ` ` `// on the last day ` ` ` `return` `count_jobs; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `N = 5; ` ` ` `vector<pair<` `int` `, ` `int` `> > ranges; ` ` ` `ranges.push_back({ 1, 5 }); ` ` ` `ranges.push_back({ 1, 5 }); ` ` ` `ranges.push_back({ 1, 5 }); ` ` ` `ranges.push_back({ 2, 3 }); ` ` ` `ranges.push_back({ 2, 3 }); ` ` ` ` ` `cout << find_maximum_jobs(N, ranges); ` `} ` |

*chevron_right*

*filter_none*

**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(N^{2})

## Recommended Posts:

- Maximum CPU Load from the given list of jobs
- Job Scheduling with two jobs allowed at a time
- Schedule jobs so that each server gets equal load
- Find minimum time to finish all jobs with given constraints
- Maximize the value of x + y + z such that ax + by + cz = n
- Maximize the sum of arr[i]*i
- Maximize the value of the given expression
- Maximize GCD of all possible pairs from 1 to N
- Maximize the Expression | Bit Manipulation
- Maximize the Sum of the given array using given operations
- Maximize the expression (A AND X) * (B AND X) | Bit Manipulation
- Maximize big when both big and small can be exchanged
- Rearrange an array to maximize i*arr[i]
- Maximize elements using another array
- Maximize the bitwise OR of an array
- Maximize array sum after K negations | Set 1
- Maximize array sum after K negations | Set 2
- Maximize the number of subarrays with XOR as zero
- Maximize the sum of sum of the Array by removing end elements
- Maximize number of elements from Array with sum at most K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.