meeting room 2

/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
class Solution {
public:

    static bool comp(pair<int, int> p1, pair<int, int> p2)
    {
        if(p1.first != p2.first)
        {
            return p1.first < p2.first;   
        }

        return p1.second > p2.second;
    }

    int minMeetingRooms(vector<Interval>& intervals) {
        int n = intervals.size();
        if(n == 0)
        {
            return 0;
        }

        vector<pair<int, int>> time;
        for(Interval i : intervals)
        {
            time.push_back(make_pair(i.start, 0));
            time.push_back(make_pair(i.end, 1));
        }

        sort(time.begin(), time.end(), comp);

        int maxroom = 0;
        int cur = 0;
        for(pair<int, int> p : time)
        {
            if(p.second == 0)
            {
                cur++;
                maxroom = max(maxroom, cur);
            }
            else
            {
                cur--;
            }
        }

        return maxroom;
    }
};

results matching ""

    No results matching ""