sort + pq 贪心解决问题

253. Meeting Rooms II 用pq思路

sort by begin, pq sort by end TODO https://leetcode.com/problems/meeting-rooms-ii/solution/

class Solution {
public:
    int minMeetingRooms(vector<vector<int>>& intervals) {
        sort(intervals.begin(), intervals.end());
        priority_queue<int, vector<int>, std::greater<int>> pq;//min
        pq.push(intervals[0][1]);
        for (int i = 1; i < intervals.size(); i++) {
            if (pq.top() <= intervals[i][0]){
                pq.pop();
            }
            pq.push(intervals[i][1]);
        }
        return pq.size();
    }
};

630. Course Schedule III

Written on December 30, 2021