Maximum Number of Events That Can Be Attended LeetCode solution in Python

1
# Runtime: 1160 ms (Top 93.22%) | Memory: 61.6 MB (Top 51.45%)
2
class Solution(object):
3
def maxEvents(self, events):
4
"""
5
:type events: List[List[int]]
6
:rtype: int
7
"""
8
events = sorted(events, key=lambda x: x[0])
9
current_day = 0
10
min_heap = []
11
event_id = 0
12
total_number_of_days = max(end for start, end in events)
13
total_events_attended = 0
14
# total_number_of_days+1 because I want to include the last day
15
for day in range(1, total_number_of_days + 1):
16

17
# Add all the events that can be started on that day
18
while event_id < len(events) and events[event_id][0] == day:
19
heapq.heappush(min_heap, events[event_id][1])
20
event_id += 1
21

22
# while there is something in heap and the event should have been completed before the current day
23
# remove those evenets and consider them not attended
24
while min_heap and min_heap[0] < day:
25
heapq.heappop(min_heap)
26

27
# if theere is an event present in heap
28
# lets mark 1 of those events as complted today and add it to
29
# total_events_attended
30

31
if min_heap:
32
heapq.heappop(min_heap)
33
total_events_attended += 1
34
return total_events_attended

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0