2025-11-20 18:30:09 +0300 MSK

Set Intersection Size At Least Two

Code

class Solution(object):
    def intersectionSizeTwo(self, intervals):
        intervals.sort(key =lambda val: (val[0], -val[1]))
        todo = [2] * len(intervals)
        ans = 0
        while intervals:
            (s, e), t = intervals.pop(), todo.pop()
            for p in range(s, s+t):
                for i, (s0, e0) in enumerate(intervals):
                    if todo[i] and p <= e0:
                        todo[i] -= 1
                ans += 1
        return ans