-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path253.py
38 lines (35 loc) · 1.2 KB
/
253.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
# https://neetcode.io/problems/meeting-schedule-ii
"""
Definition of Interval:
"""
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
from typing import List
import heapq
class Solution: # O(n^2) time, O(n) space
def minMeetingRooms(self, intervals: List[Interval]) -> int:
if not intervals:
return 0
intervals.sort(key=lambda interval: interval.start)
days = [intervals[0]]
for interval in intervals[1:]:
fitted = False
for i, day in enumerate(days):
if interval.start >= day.end:
days[i] = Interval(day.start, interval.end)
fitted = True
break
if not fitted:
days.append(interval)
return len(days)
class Solution: # O(nlogn) time, O(n) space
def minMeetingRooms(self, intervals: List[Interval]) -> int:
intervals.sort(key=lambda interval: interval.start)
days = []
for interval in intervals:
if days and days[0] <= interval.start:
heapq.heappop(days)
heapq.heappush(days, interval.end)
return len(days)