Leetcode 1478 Solution

This article provides solution to leetcode question 1478 (maximum-number-of-events-that-can-be-attended)

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

Solution

class Solution:
    def maxEvents(self, events: List[List[int]]) -> int:
        events.sort()

        begin_day = events[0][0]
        end_day = max([end_day for _, end_day in events])

        day = begin_day
        q = []
        i = 0
        ans = 0

        while day <= end_day:
            while i < len(events) and events[i][0] <= day:
                heapq.heappush(q, events[i][1])
                i += 1

            if q:
                heapq.heappop(q)
                ans += 1

            while q and q[0] <= day:
                heapq.heappop(q)

            day += 1

        return ans