會議室 II

難度:中等
類型:數組
題意:找到最少使用的會議室數,可以用上下車的思維來考慮。會議室的開始時間爲上車,會議室的結束時間爲下車,我們需要統計在車上的人的最大數量。按照時間的先後順序排序,注意如果在同一個時刻既有上車又有下車的人,先下後上,騰出最大的空間。隨意排序的時候加入了第二個關鍵字,就是爲了防止上車的先算。

class Solution {
public:
    int minMeetingRooms(vector<vector<int>>& intervals) {
        vector<pair<int, int>> temp;
        for(auto interval: intervals){
            temp.push_back({interval[0], 1});
            temp.push_back({interval[1], -1});
        }
        sort(temp.begin(), temp.end());
        int cnt = 0, res = 0;
        for(auto it: temp){
            if(it.second > 0) cnt++;
            else cnt--;
            res = max(res, cnt);
        }
        return res;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章