差分数组

class Solution {
public:

int minMeetingRooms(vector<vector<int>>& a) {
int maxn = 0;

for(auto& x:a){
maxn = std::max<int>(maxn,x[0]);
maxn = std::max<int>(maxn,x[1]);
}
maxn += 1;
vector<int> s (maxn,0);
for(auto& x:a){
++s [x[0]];
-- s[x[1]];
}
int ret = s[0];
for(int i=1;i<maxn;++i){
s[i] += s[i-1];
ret = max<int>(ret,s[i]);
}
return ret;
}
};

会议起点和终点解耦

​https://leetcode.cn/problems/meeting-rooms-ii/solution/hui-yi-shi-ii-by-leetcode/​

class Solution {
public:

int minMeetingRooms(vector<vector<int>>& a) {
int n = a.size();
vector<int> s(n,0);
vector<int> e(n,0);

for(int i=0;i<a.size();++i){
s[i] = a[i][0];
e[i] = a[i][1];
}

sort(s.begin(),s.end());
sort(e.begin(),e.end());

int ret = 1;
int j=0;
for(int i=1;i<n;++i){
if( e[j] <= s[i] ){
++j;
}else{
++ret;
}
}
return ret;
}
};

【LeetCode】253. 会议室 II_算法