Участник:Alexryabov/TaskIntersectionSizeAtLeastTwo

Материал из DISCOPAL
Перейти к: навигация, поиск

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

class Solution(object):
    def intersectionSizeTwo(self, intervals):
        intervals.sort(key = lambda x:x[1]) # отсортируем множество "интервалов" по конечным точкам
        min_size = 0
        previous = []
        for (start, end) in intervals:
            if not previous or previous[1] < start:
                min_size += 2
                previous = [end-1, end]
            elif previous[0] < start:
                previous = [previous[1], end]
                min_size += 1
        return min_size