Leetcode 759 Solution

This article provides solution to leetcode question 759 (set-intersection-size-at-least-two)

https://leetcode.com/problems/set-intersection-size-at-least-two

Solution

class Solution:
    def intersectionSizeTwo(self, intervals: List[List[int]]) -> int:
        intervals.sort(key=lambda x: (x[1], -x[0]))

        left = intervals[0][1] - 1
        right = left + 1
        ans = 2

        for i in range(1, len(intervals)):
            a, b = intervals[i]

            if a <= left:
                continue
            elif a <= right:
                left = right
                right = b
                ans += 1
            else:
                left = b - 1
                right = b
                ans += 2

        return ans