Solution using priority_queue(minHeap) in c++


#1
int Solution::solve(vector<vector<int> > &time) {
     sort(time.begin(),time.end());
    //now our interval are sorted based on start time
    //since we have to count the number of rooms required
    //we may use minHeap for this
    priority_queue<int,vector<int>,greater<int>> minHeap;
    //now push the first end time in the heap and iterate for rest
    //edge case
    if(time.size() == 0)
        return 0;
    minHeap.push(time[0][1]);
    for(int i = 1; i < time.size(); i++) {
        //check if the end time is smaller or equal then the current start time
        //the we can pop the heap and the current's end time
        if(time[i][0] >= minHeap.top()) {
            minHeap.pop();
        }
        minHeap.push(time[i][1]);
    }
    //now our required rooms will be heap size
return minHeap.size();
}