test cases not satisfying
Plz correct this
Hey @devwrath1999 the approach you have used is not correct. Follow this to get an idea of the solution
The idea is to first sort the array based on start time, so we can examine the meetings as they take place in order (and be greedy).
Suppose we have three meetings: [0, 30], [5, 10], and [15, 20] in sorted order. When we see the second meeting, we check if its start time is later than the first meeting’s end time. It’s not, so we need another room. We then compare the third meeting’s start time with the minimum of first two meetings’ end times. If there is a meeting that ends before the third meeting starts, then we don’t need another room.
So as we iterate through the array, we need to store each meeting’s end time and get the minimum quickly. Min heap is a natural choice.
Code for the same is given below
struct Interval {
int start;
int end;
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
};
int minPartyHall(vector<Interval>& intervals) {
if (intervals.size() == 0) {
return 0;
}
sort(intervals.begin(), intervals.end(), [](const Interval& i1, const Interval& i2) -> bool {
return i1.start < i2.start;
});
priority_queue<int, vector<int>, greater<int>> heap;
int rooms = 0;
heap.push(intervals[0].end);
rooms++;
for (int i = 1; i < intervals.size(); i++) {
if (intervals[i].start < heap.top()) {
rooms++;
} else {
heap.pop();
}
heap.push(intervals[i].end);
}
return rooms;
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.