区间类型的第5类题型 (区间分组)
思路:先按照左边界从小到大排列,然后判断之前组别的最小值是否小于等于该元素左边界,如果小于,则更新。否则成立新的组。
细节实现:用小根堆维护组的最小值。
class Solution {
public:
int minmumNumberOfHost(int n, vector<vector<int> >& startEnd) {
priority_queue<int, vector<int>, greater<int>> pq;
sort(startEnd.begin(), startEnd.end());
for (auto& each : startEnd) {
if (pq.empty()) pq.push(each[1]);
else {
if (each[0] >= pq.top()) {
pq.pop();
pq.push(each[1]);
}else {
pq.push(each[1]);
}
}
}
return pq.size();
}
};