# Maximum houses that can be painted continuously when the painting duration and date limit is given

• Difficulty Level : Hard
• Last Updated : 15 Sep, 2021

Given an array arr[][] consisting of N pairs such that each pair {L, R} represents that ith house can be painted in L number of days before the Rth day, the task is to find the maximum number of house that can be painted continuously.
Examples:

Input: N = 4, paint[ ][ ] = [[1, 19], [2, 2], [4, 17], [1, 1]]
Output: 3
Explanation: Maximum of three houses can be painted and order is {4, 3, 1}

Input: N = 4, paint[ ][ ] = [[100, 210], [200, 1310], [1000, 1275], [2500, 3000]]
Output: 3

Approach: The problem can be solved using the greedy approach. The idea is to choose from the lowest DaysRequired within the current LastDay. Follow the steps below for the approach.

• Initialize a vector, say V, to store all the pairs which can be taken.
• Sort the vector V by using a comparator pair.second less than pair.first
• Initialize a priority queue pq and push the first pair of the vector V.
• Initialize a variable, say t, to store the time.
• Traverse the vector, V and put the current pair in priority queue pq:
• If t + DaysRequired is less than or equal to LastDay, then continue.
• else, pop from the priority queue, store in temp variable and also update t equals t – temp.first
• Finally, return the size of the priority queue.

Below is the implementation of the above approach:

## C++14

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `typedef` `vector > Matrix;` `// Comparator for sorting``static` `bool` `cmp(pair<``int``, ``int``> a, pair<``int``, ``int``> b)``{``    ``return` `a.second < b.second;``}` `// Function to count number of houses``// that can be painted``void` `countMaxPinted(``int` `n, Matrix& paint)``{` `    ``// Vector to store the pairs``    ``vector > V;``    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// If house can be painted``        ``if` `(paint[i][0] <= paint[i][1]) {``            ``V.push_back(make_pair(paint[i][0], paint[i][1]));``        ``}``    ``}` `    ``// Sort the vector``    ``sort(V.begin(), V.end(), cmp);` `    ``// If vector is empty``    ``if` `(V.size() == 0) {``        ``cout << 0 << endl;``        ``return``;``    ``}` `    ``// Initialize t``    ``int` `t = V[0].first;` `    ``// Initialize priority queue``    ``priority_queue > pq;``    ``pq.push(V[0]);` `    ``// Traversing the vector``    ``for` `(``int` `i = 1; i < V.size(); i++) {``        ``t += V[i].first;` `        ``// Pushing in Vectors``        ``pq.push(V[i]);``        ``if` `(t <= V[i].second) {``            ``continue``;``        ``}``        ``else` `{` `            ``// Pop the top element``            ``auto` `temp = pq.top();``            ``pq.pop();``            ``t = t - temp.first;``        ``}``    ``}` `    ``// Print the ans``    ``cout << pq.size() << endl;``}` `// Driver Code``int` `main()``{` `    ``// Given Input``    ``int` `n = 4;``    ``Matrix paint = { { 1, 19 }, { 2, 2 }, { 4, 17 }, { 1, 1 } };` `    ``// Function Call``    ``countMaxPinted(n, paint);``}`

Output

`3`

Time Complexity: O(NlogN)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up