Set Intersection Size At Least Two

Problem Id: 757 Difficulty: Hard


Intuition

Solution


class Solution:
    def intersectionSizeTwo(self, intervals: List[List[int]]) -> int:
        intervals.sort(key=lambda x: x[1])
        nums = []
        for start, end in intervals:
            count = 0
            if len(nums) >= 1 and start <= nums[-1] <= end:
                count += 1
            if len(nums) >= 2 and start <= nums[-2] <= end:
                count += 1
            if count == 0:
                nums.append(end - 1)
                nums.append(end)
            elif count == 1:
                nums.append(end)

        return len(nums)