-
Notifications
You must be signed in to change notification settings - Fork 22
/
919.meeting-rooms-ii.py
66 lines (61 loc) · 1.52 KB
/
919.meeting-rooms-ii.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Tag: Heap, Greedy, Sweep Line, Sort
# Time: O(NlogN)
# Space: O(N)
# Ref: Leetcode-253
# Note: -
# 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.
#
# **Example1**
#
# ```
# Input: intervals = [(0,30),(5,10),(15,20)]
# Output: 2
# Explanation:
# We need two meeting rooms
# room1: (0,30)
# room2: (5,10),(15,20)
# ```
#
# **Example2**
#
# ```
# Input: intervals = [(2,7)]
# Output: 1
# Explanation:
# Only need one meeting room
# ```
#
# (0,8),(8,10) is not conflict at 8
from typing import (
List,
)
from lintcode import (
Interval,
)
"""
Definition of Interval:
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
"""
class Solution:
"""
@param intervals: an array of meeting time intervals
@return: the minimum number of conference rooms required
"""
def min_meeting_rooms(self, intervals: List[Interval]) -> int:
# Write your code here
meetings = []
for x in intervals:
meetings.append((x.start, 1))
meetings.append((x.end, -1))
meetings.sort()
count = 0
res = 0
n = len(meetings)
for i in range(n):
count += meetings[i][1]
if i == n - 1 or meetings[i][0] != meetings[i + 1][0]: # second -1 comes first after sorting, this is not necessary
res = max(res, count)
return res