fromsortedcontainersimportSortedListclassTweetCounts:def__init__(self):self.tweetNameToTimes=collections.defaultdict(SortedList)defrecordTweet(self,tweetName:str,time:int)->None:self.tweetNameToTimes[tweetName].add(time)defgetTweetCountsPerFrequency(self,freq:str,tweetName:str,startTime:int,endTime:int)->list[int]:counts=[]times=self.tweetNameToTimes[tweetName]chunk=60iffreq=='minute'else3600iffreq=='hour'else86400# I := startTime of each chunkforiinrange(startTime,endTime+1,chunk):j=min(i+chunk,endTime+1)# EndTime of each chunkcounts.append(bisect_left(times,j)-bisect_left(times,i))returncounts
Thanks for stopping by! If you find this site helpful, consider buying me some protein powder to keep me fueled! π