Meeting Schedule II
Given an array of meeting time interval objects consisting of start and end times [[start_1,end_1],[start_2,end_2],…] (start_i < end_i), find the minimum number of days required to schedule all meetings without any conflicts.
Example 1:
Input: intervals = [(0,40),(5,10),(15,20)]
Output: 2
Explanation:
day1: (0,40)
day2: (5,10),(15,20)
Example 2:
Input: intervals = [(4,9)]
Output: 1
Note:
(0,8),(8,10) is not considered a conflict at 8
Constraints:
0 <= intervals.length <= 500
0 <= intervals[i].start < intervals[i].end <= 1,000,000
Solution
A commonly used trick in overlapping interval problems is that give the left endpoints with weight 1 and right end points with weight -1. Then we can sort these weighted endpoints in ascending order and traverse them to sum up the weights. During the traversal, the value of current sum is equal to the number of current unclosed overlapping intervals.
Code
"""
Definition of Interval:
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
"""
class Solution:
def minMeetingRooms(self, intervals: List[Interval]) -> int:
time = []
for interval in intervals:
time.append((interval.start, 1))
time.append((interval.end, -1))
time.sort()
ans = 0
pre = 0
for t in time:
pre += t[1]
ans = max(ans, pre)
return ans