Участник:UlitinAleksander/maximum-number-of-events-that-can-be-attended

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

python3

https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended

from heapq import heappop, heappush
 
class Solution:
    def maxEvents(self, events: List[List[int]]) -> int:
        events = sorted(events)
        start = 0
        on_heap = 0
        day = 0
        h = []
        attended, missed = [], []
        while start < len(events):
            day = max(events[start][0], day + 1)
            while on_heap < len(events) and events[on_heap][0] <= day:
                e = events[on_heap]
                heappush(h, (e[1], e))
                on_heap += 1
 
 
            cont = True
            while cont and h:
                attend = heappop(h)
                if attend[1][1] >= day:
                    attended.append(attend[1])
                    cont = False
                else:
                    missed.append(attend[1])
                    cont = True
            start += 1
        return len(attended)