Meeting Rooms
https://leetcode.com/problems/meeting-rooms/
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
static bool mysort(const pair<int, int>& p1, const pair<int, int>& p2)
{
if(p1.first != p2.first)
{
return p1.first < p2.first;
}
return p1.second > p2.second;
}
bool canAttendMeetings(vector<Interval>& intervals) {
int n = intervals.size();
if(n <= 1)
{
return true;
}
vector<pair<int, int>> time;
for(Interval i : intervals)
{
time.push_back(make_pair(i.start, 0));
time.push_back(make_pair(i.end, 1));
}
//cout<<time.size()<<endl;
sort(time.begin(), time.end(), mysort);
//cout<<time.size()<<endl;
bool busy = false;
for(pair<int, int> p : time)
{
//cout<<p.first<<" "<<p.second<<endl;
if(p.second == 0)
{
if(busy == true)
{
return false;
}
busy = true;
}
else
{
busy = false;
}
}
return true;
}
};