https://leetcode-cn.com/problems/meeting-scheduler/
两个人有各有很多有空的区间。找出两个人有空的第一个区间。就是找两个区间集合的第一个符合要求的交集。用upper_bound-1找出第一个小于等于当前区间的区间。用lower_bound找出第一个大于等于当前区间的区间。看看两个区间符不符合要求即可。
class Solution {
public:
set<pair<int, int>> times;
vector<int> minAvailableDuration(vector<vector<int>>& slots1, vector<vector<int>>& slots2, int duration) {
for(auto vec : slots1)
times.insert({vec[0], vec[1]});
sort(slots2.begin(), slots2.end());
for(auto vec : slots2)
{
pair<int, int> p({vec[0], vec[1]});
auto lo = times.upper_bound(p); // <= p 的第一个区间
auto hi = times.lower_bound(p); // >= p 的第一个区间
lo--;
if(lo != end(times))
{
int left = max(lo->first, p.first);
int right = min(lo->second, p.second);
if(right - left >= duration)
return {left, left + duration};
}
if(hi != end(times))
{
int left = max(hi->first, p.first);
int right = min(hi->second, p.second);
if(right - left >= duration)
return {left, left + duration};
}
}
return {};
}
};