Skip to content

Latest commit

 

History

History
26 lines (24 loc) · 688 Bytes

757.md

File metadata and controls

26 lines (24 loc) · 688 Bytes

757. Set Intersection Size At Least Two

Solution 1 (time O(nlog(n)), space O(n))

class Solution(object):
    def intersectionSizeTwo(self, intervals):
        """
        :type intervals: List[List[int]]
        :rtype: int
        """
        intervals.sort(key=lambda x: x[1])
        ans = 0
        cur = []
        for s, e in intervals:
            if not cur or s > cur[1]:
                ans += 2
                cur = [e - 1, e]
            elif s > cur[0]:
                ans += 1
                if cur[1] == e:
                    cur = [e - 1, e]
                else:
                    cur = [cur[1], e]
        return ans