forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeetingRoomsII.swift
37 lines (32 loc) · 1.03 KB
/
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
36
37
/**
* 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: [[Int]]) -> Int {
let startingTimes = intervals.map { interval in interval[0] }.sorted()
let endingTimes = intervals.map { interval in interval[1] }.sorted()
let intervalsCount = intervals.count
var i = 0, j = 0, meetingRoomsNum = 0
while i < intervalsCount && j < intervalsCount {
if startingTimes[i] < endingTimes[j] {
meetingRoomsNum += 1
} else {
j += 1
}
i += 1
}
return meetingRoomsNum
}
}