You are given a 2D integer array intervals where intervals[i] = [lefti, righti]
represents the inclusive interval [lefti, righti].
You have to divide the intervals into one or more groups
such that each interval is in exactly one group,
and no two intervals that are in the same group intersect each other.
Return the minimum number of groups you need to make.
Two intervals intersect if there is at least one common number between them.
For example, the intervals [1, 5] and [5, 8] intersect.
[[5,10],[6,8],[1,5],[2,3],[1,10]]
class Solution:
def minGroups(self, intervals: List[List[int]]) -> int:
intervals.sort()
endingTimes = []
for start, end in intervals:
if not endingTimes:
heappush(endingTimes, end)
else:
minimumEnd = heappop(endingTimes)
# 겹칠 경우 다시 그룹에 추가된다.
if minimumEnd >= start:
heappush(endingTimes, minimumEnd)
# 겹치지 않을때, 겹쳤을때 모두 end 그룹이 생겨나긴 한다.
# 겹쳤을때는 새로운 그룹
# 겹치지 않았을때는 원래 그룹이 업데이트
heappush(endingTimes, end)
return len(endingTimes)