<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>

from sortedcontainers import SortedDict

class Solution:

    def maxEvents(self, A):
        A.sort(reverse=1)

        h = []
        d = 0
        r = 0
        while A or h:
            if not h:
                d = A[-1][0]
            
            while A and A[-1][0] <= d:
                heapq.heappush(h, A.pop()[1])
            
            heapq.heappop(h)

            d += 1
            r += 1

            while h and h[0] < d:
                heapq.heappop(h)

        return r



































        # A.sort(reverse=1)

        # print(A)

        # h = []
        # res = d = 0
        # while A or h:
        #     if not h: 
        #         d = A[-1][0]

        #     while A and A[-1][0] <= d:
        #         heapq.heappush(h, A.pop()[1])
                
        #     heapq.heappop(h)
        #     res += 1
        #     d += 1

        #     while h and h[0] < d:
        #         heapq.heappop(h)

        # return res

    # def maxEvents(self, events: List[List[int]]) -> int:

    #     grouped = SortedDict()
    #     for e in events:
    #         if e[0] in grouped:
    #             grouped[e[0]].append(e)
    #         else:
    #             grouped[e[0]] = [e]

    #     sdict = SortedDict()
    #     for k in grouped:
    #         values = grouped[k]            
    #         values.sort(key=lambda e: e[1] - e[0])

    #         event = values[0]
    #         duration = event[1] - event[0]

    #         sdict[(k, duration)] = values
        

    #     day = 0
    #     counter = 0

    #     while sdict:

    #         key = sdict.peekitem(0)[0]
    #         events = sdict.pop(key=key)

    #         firstEvent = events[0]

    #         if day <= firstEvent[1]:
    #             day = max(day, firstEvent[0]) + 1
    #             counter += 1

    #             events.pop(0)

    #             if events:
    #                 nextEventStart = events[0][0]
    #                 nextEventEnd = events[0][1]

    #                 duration = nextEventEnd - nextEventStart

    #                 sdict[(day, duration)] = events


            
    #     return counter
        #events = sorted(events, key=lambda e: (e[1] - e[0], e[0], e[1]))
        #print(events)
        #return 0

        # events = sorted(events)

        # prev = [-1, -1]
        # same_start_offset = 0
        
        # for e in events:
        #     if prev[0] == e[0]:
        #         same_start_offset += 1
        #         e[0] += same_start_offset

        #         if e[0] > e[1]:
        #             e[0] = -1
        #             e[1] = -1
            
        #     else:
        #         same_start_offset = 0
        #         prev = e

        # events = sorted(events)
        # print(events)
        
        # count = 0
        # day = 0
        # for e in events:
        #     if day <= e[1]:
        #         count += 1
        #         day = max(e[0], day) + 1


        # return count
        #events = sorted(events) # , key=lambda e: e[1]
        #print(events)





        # heapq.heapify(events)

        # day = 0
        # counter = 0
        # while events:
        #     e = heapq.heappop(events)
        #     if day > e[1]:
        #         continue

        #     counter += 1
        #     day = max(day, e[0]) + 1

        #     while events and events[0][0] == e[0]:
        #         next_event = heapq.heappop(events)
        #         if next_event[1] < day:
        #             continue

        #         next_event[0] = day
        #         heapq.heappush(events, next_event)
            
        # return counter




        #for e in events[1: ]:


            #heapq.h
            
        #return count

    # def maxEvents(self, events: List[List[int]]) -> int:
    #     events.sort(key=lambda x: (x[1], x[0]))
    #     attended_days = set()
    #     for start, end in events:
    #         for day in range(start, end + 1):
    #             if day not in attended_days:
    #                 attended_days.add(day)
    #                 break
    #     return len(attended_days)
52600