forked from banjodayo39/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeetingRoomsII.swift
35 lines (32 loc) · 958 Bytes
/
MeetingRoomsII.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
/**
* Question Link: https://leetcode.com/problems/meeting-rooms-ii/
* Primary idea: Sort start and end separately, then count conflicts
* Time Complexity: O(nlogn), Space Complexity: O(n)
*
* 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 MeetingRoomsII {
func minMeetingRooms(_ intervals: [Interval]) -> Int {
let starts = intervals.map { interval in interval.start }.sorted()
let ends = intervals.map { interval in interval.end }.sorted()
var i = 0, j = 0, count = 0
while i < starts.count && j < ends.count {
if starts[i] < ends[j] {
count += 1
i += 1
} else {
i += 1
j += 1
}
}
return count
}
}