12ms, priority queue C++


  • 0
    T
    class Solution {
    public:
        int minMeetingRooms(vector<Interval>& intervals) {
    
            std::sort(intervals.begin(),intervals.end(),[](const Interval& a, const Interval& b){return a.start != b.start ? a.start < b.start : a.end < b.end;});
            
            int max_interval=0;
            std::priority_queue<Interval,std::vector<Interval>,function<bool(const Interval& a, const Interval& b)>> pq([](const Interval& a, const Interval& b){return a.end > b.end;});
            
            int next=0;
            size_t max_meetings=0;
            while(next<intervals.size())
            {
                if(pq.empty()) pq.push(intervals[next++]); 
                while(next<intervals.size() && intervals[next].start<pq.top().end) pq.push(intervals[next++]);
                max_meetings = std::max(pq.size(),max_meetings);
                pq.pop();
            }
            return max_meetings;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.