aboutsummaryrefslogtreecommitdiff
path: root/divide_interval_min_group.py
blob: c8c1ed80f92aec52ecb8982a2f92c6b6fc32d84d (plain)
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
# 2406. Divide Intervals Into Minimum Number of Groups

"""
you are given a 2d integer array 'intervals' where 'intervals[i] = [left,
right]' represents the inclusive interval '[left, right]'. 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.
"""


class Solution(object):
    def minGroups(self, intervals):
        """
        :type intervals: List[List[int]]
        :rtype: int
        """
        arr = []
        for i, j in intervals:
            arr.append([i, 1])
            arr.append([j + 1, -1])
        ans, curr = 0, 0
        for i, j in sorted(arr):
            curr += j
            ans = max(ans, curr)
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.minGroups(intervals=[[5, 10], [6, 8], [1, 5], [2, 3], [1, 10]]))
    print(obj.minGroups(intervals=[[1, 3], [5, 6], [8, 10], [11, 13]]))