forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
46 lines (35 loc) · 990 Bytes
/
main2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/// Source : https://leetcode.com/problems/meeting-rooms/description/
/// Author : liuyubobobo
/// Time : 2018-09-10
#include <iostream>
#include <vector>
using namespace std;
/// Sorting and see if overlapped linearly
/// Time Complexity: O(nlogn)
/// Space Complexity: O(1)
/// 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:
bool canAttendMeetings(vector<Interval>& intervals) {
sort(intervals.begin(), intervals.end(), cmpIntervals);
for(int i = 1; i < intervals.size(); i ++)
if(intervals[i].start < intervals[i - 1].end)
return false;
return true;
}
private:
static bool cmpIntervals(const Interval& i1, const Interval& i2){
if(i1.start != i2.start)
return i1.start < i2.start;
return i1.end < i2.end;
}
};
int main() {
return 0;
}