fromsortedcontainersimportSortedListclassTweetCounts:def__init__(self):self.d={"minute":60,"hour":3600,"day":86400}self.data=defaultdict(SortedList)defrecordTweet(self,tweetName:str,time:int)->None:self.data[tweetName].add(time)defgetTweetCountsPerFrequency(self,freq:str,tweetName:str,startTime:int,endTime:int)->List[int]:f=self.d[freq]tweets=self.data[tweetName]t=startTimeans=[]whilet<=endTime:l=tweets.bisect_left(t)r=tweets.bisect_left(min(t+f,endTime+1))ans.append(r-l)t+=freturnans# Your TweetCounts object will be instantiated and called as such:# obj = TweetCounts()# obj.recordTweet(tweetName,time)# param_2 = obj.getTweetCountsPerFrequency(freq,tweetName,startTime,endTime)
classTweetCounts{privateMap<String,TreeMap<Integer,Integer>>data=newHashMap<>();publicTweetCounts(){}publicvoidrecordTweet(StringtweetName,inttime){data.putIfAbsent(tweetName,newTreeMap<>());vartm=data.get(tweetName);tm.put(time,tm.getOrDefault(time,0)+1);}publicList<Integer>getTweetCountsPerFrequency(Stringfreq,StringtweetName,intstartTime,intendTime){intf=60;if("hour".equals(freq)){f=3600;}elseif("day".equals(freq)){f=86400;}vartm=data.get(tweetName);List<Integer>ans=newArrayList<>();for(inti=startTime;i<=endTime;i+=f){ints=0;intend=Math.min(i+f,endTime+1);for(intv:tm.subMap(i,end).values()){s+=v;}ans.add(s);}returnans;}}/** * Your TweetCounts object will be instantiated and called as such: * TweetCounts obj = new TweetCounts(); * obj.recordTweet(tweetName,time); * List<Integer> param_2 = obj.getTweetCountsPerFrequency(freq,tweetName,startTime,endTime); */
classTweetCounts{public:TweetCounts(){}voidrecordTweet(stringtweetName,inttime){data[tweetName].insert(time);}vector<int>getTweetCountsPerFrequency(stringfreq,stringtweetName,intstartTime,intendTime){intf=60;if(freq=="hour")f=3600;elseif(freq=="day")f=86400;vector<int>ans((endTime-startTime)/f+1);autol=data[tweetName].lower_bound(startTime);autor=data[tweetName].upper_bound(endTime);for(;l!=r;++l){++ans[(*l-startTime)/f];}returnans;}private:unordered_map<string,multiset<int>>data;};/** * Your TweetCounts object will be instantiated and called as such: * TweetCounts* obj = new TweetCounts(); * obj->recordTweet(tweetName,time); * vector<int> param_2 = obj->getTweetCountsPerFrequency(freq,tweetName,startTime,endTime); */