Last active
July 9, 2023 06:22
-
-
Save tnvmadhav/d88d969f6bc53fc8fd417ca9f4105d44 to your computer and use it in GitHub Desktop.
Finding Peak Audience Count for a given list of events by start and end times (integers)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
class ManageIntervals: | |
def __init__(self): | |
self.my_dict = defaultdict(int) | |
self.mx = 0 | |
# O(1) | |
def add_new_interval(self, start_time: int, end_time: int, audience_count: int): | |
self.my_dict[start_time] += audience_count | |
self.my_dict[end_time] -= audience_count | |
# O(NLogN) | |
def get_peak_audience(self): | |
sum = 0 | |
# O(N) + O(NLogN) | |
for _, v in sorted(self.my_dict.items()): | |
sum += v | |
self.mx = max(self.mx, sum) | |
return self.mx | |
if __name__ == "__main__": | |
mi = ManageIntervals() | |
# O(1) | |
mi.add_new_interval(1688107476, 1688807476, 100) | |
# O(1) | |
mi.add_new_interval(1645107476, 1620107476, 200) | |
# O(1) | |
mi.add_new_interval(1688057476, 1688027476, 300) | |
# O(1) | |
mi.add_new_interval(1668107476, 1688107466, 10) | |
# O(NLogN) | |
print(mi.get_peak_audience()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment