forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeetingRooms.swift
39 lines (35 loc) · 981 Bytes
/
MeetingRooms.swift
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
/**
* Question Link: https://leetcode.com/problems/meeting-rooms/
* Primary idea: Sort and traverse, find if any conflicts exist
* Time Complexity: O(nlogn), Space Complexity: O(1)
*
* Definition for an interval.
* public class Interval {
* public var start: Int
* public var end: Int
* public init(_ start: Int, _ end: Int) {
* self.start = start
* self.end = end
* }
* }
*/
class MeetingRooms {
func canAttendMeetings(intervals: [Interval]) -> Bool {
guard intervals.count > 1 else {
return true
}
var intervals = intervals.sort() {
if $0.start != $1.start {
return $0.start < $1.start
} else {
return $0.end < $1.end
}
}
for i in 0..<intervals.count - 1 {
if intervals[i].end > intervals[i + 1].start {
return false
}
}
return true
}
}