<p>You are given an array of <code>events</code> where <code>events[i] = [startDay<sub>i</sub>, endDay<sub>i</sub>]</code>. Every event <code>i</code> starts at <code>startDay<sub>i</sub></code><sub> </sub>and ends at <code>endDay<sub>i</sub></code>.</p>

<p>You can attend an event <code>i</code> at any day <code>d</code> where <code>startTime<sub>i</sub> &lt;= d &lt;= endTime<sub>i</sub></code>. You can only attend one event at any time <code>d</code>.</p>

<p>Return <em>the maximum number of events you can attend</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/05/e1.png" style="width: 400px; height: 267px;" />
<pre>
<strong>Input:</strong> events = [[1,2],[2,3],[3,4]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can attend all the three events.
One way to attend them all is as shown.
Attend the first event on day 1.
Attend the second event on day 2.
Attend the third event on day 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> events= [[1,2],[2,3],[3,4],[1,2]]
<strong>Output:</strong> 4
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= events.length &lt;= 10<sup>5</sup></code></li>
	<li><code>events[i].length == 2</code></li>
	<li><code>1 &lt;= startDay<sub>i</sub> &lt;= endDay<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    def maxEvents(self, events: List[List[int]]) -> int:
        n = len(events)
        events = sorted(events, key = lambda x: (x[0], x[1]))
        heapq.heapify(events)

        started = []
        count, i = 0, 0
        curr_day = events[0][0]

        # While some event hasn't been added to heap
        while i<n:
            # Add all events that start on curr_day
            while i<n and events[i][0]==curr_day:
                heappush(started, events[i][1])
                i += 1

            # Attend started event that ends earliest
            heappop(started)
            count += 1
            curr_day += 1
            
            # Remove all expired events
            while started and started[0]<curr_day:
                heappop(started)
            # If no started events left, move to the next startDay
            if i<n and not started:
                curr_day = events[i][0]

        # Events that started that are still left in heap
        while started:
            # Non-expired started event that ends earliest
            if heappop(started)>=curr_day:
                curr_day += 1
                count += 1

        return count
65600