-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path920.meeting-rooms.cpp
65 lines (60 loc) · 1.55 KB
/
920.meeting-rooms.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Tag: Sort
// Time: O(NlogN)
// Space: O(1)
// Ref: Leetcode-252
// Note: -
// Given an array of meeting time intervals consisting of start and end times `[(s1,e1),(s2,e2),...] (si < ei)`, determine if a person could attend all meetings.
//
// **Example1**
//
// ```
// Input: intervals = [(0,30),(5,10),(15,20)]
// Output: false
// Explanation:
// (0,30), (5,10) and (0,30),(15,20) will conflict
// ```
//
// **Example2**
//
// ```
// Input: intervals = [(5,8),(9,15)]
// Output: true
// Explanation:
// Two times will not conflict
// ```
//
// - $0 \leq intervals .length \leq 10^4$
// - $intervals[i].length == 2$
// - $0 \leq start_i < end_i \leq 10^6$
// - `[(0,8), (8,10)]` is not conflict at **8**
/**
* Definition of Interval:
* class Interval {
* public:
* int start, end;
* Interval(int start, int end) {
* this->start = start;
* this->end = end;
* }
* }
*/
class Solution {
public:
/**
* @param intervals: an array of meeting time intervals
* @return: if a person could attend all meetings
*/
bool canAttendMeetings(vector<Interval> &intervals) {
// Write your code here
sort(intervals.begin(), intervals.end(), [](Interval &a, Interval &b) {
return (a.start < b.start) || (a.start == b.start && a.end < b.end);
});
int n = intervals.size();
for (int i = 1; i < n; i++) {
if (intervals[i].start < intervals[i - 1].end) {
return false;
}
}
return true;
}
};