-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
253 Meeting Rooms II.swift
42 lines (34 loc) 路 1.05 KB
/
253 Meeting Rooms II.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
40
41
42
//
// 253 Meeting Rooms II.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 15/10/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/meeting-rooms-ii/
class Solution {
/// Finds the minimum number of conference rooms needed for meetings.
///
/// - Parameter intervals: An array of meeting time intervals.
/// - Returns: The minimum number of conference rooms.
///
/// - Complexity:
/// - time: O(n log(n)), where n is the number of intervals.
/// - space: O(n), where n is the number of intervals.
func minMeetingRooms(_ intervals: [[Int]]) -> Int {
guard !intervals.isEmpty else { return 0 }
let start = intervals.map { $0[0] }.sorted()
let end = intervals.map { $0[1] }.sorted()
var ans = 0
var j = 0
for i in 0..<start.count {
if start[i] < end[j] {
ans += 1
} else {
j += 1
}
}
return ans
}
}