[Swift]LeetCode253.会议室 II $ Meeting Rooms II

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.

For example,
Given [[0, 30],[5, 10],[15, 20]],
return 2.


给定由开始和结束时间 [[s1,e1],[s2,e2],...] (si < ei)组成的一系列会议时间间隔,找出所需的最小会议室数。

例如,

给出 [[0, 30],[5, 10],[15, 20]],

返回2。


 1 class Solution {
 2     func canAttendMeetings(_ intervals:[[Int]]) -> Int {
 3         var intervals = intervals
 4         intervals.sort(by: {(arr1:[Int],arr2:[Int]) -> Bool in
 5                     return arr1.first! < arr2.first!})
 6         var q:[Int] = [Int]()
 7         for a in intervals
 8         {
 9             if !q.isEmpty && q.last! <= a.first!
10             {
11                 for (index, value) in q.enumerated()
12                 {
13                     if q.min() == value
14                     {
15                         q.remove(at: index)
16                         break
17                     }
18                 }
19             }
20             q.append(a.last!)
21         }
22         return q.count
23     }
24 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10218124.html
今日推荐