1348. Tweet Counts Per Frequency
https://leetcode.com/problems/tweet-counts-per-frequency/
Implement the class TweetCounts
that supports two methods:
1. recordTweet(string tweetName, int time)
Stores the
tweetName
at the recordedtime
(in seconds).
2. getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime)
Returns the total number of occurrences for the given
tweetName
per minute, hour, or day (depending onfreq
) starting from thestartTime
(in seconds) and ending at theendTime
(in seconds).freq
is always minute, hour or day, representing the time interval to get the total number of occurrences for the giventweetName
.The first time interval always starts from the
startTime
, so the time intervals are[startTime, startTime + delta*1>, [startTime + delta*1, startTime + delta*2>, [startTime + delta*2, startTime + delta*3>, ... , [startTime + delta*i,
min
(startTime + delta*(i+1), endTime + 1)>
for some non-negative numberi
anddelta
(which depends onfreq
).
Example:
Constraints:
There will be at most
10000
operations considering bothrecordTweet
andgetTweetCountsPerFrequency
.0 <= time, startTime, endTime <= 10^9
0 <= endTime - startTime <= 10^4
实现两个api,recordTweet(string tweetName, int time)记录在什么时刻(单位s)有什么tweet,getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime)统计对于给定tweet,从startTime到endTime内以分或小时或天为间隔,出现的次数。time-based的遍历=>tree map。对于每个词维护一个的treemap,并在查询时根据interval划分出buckets,再从startTime开始遍历tree map并根据当前时间更新对应的bucket里的频率,直到超过endTime。
Last updated
Was this helpful?