Tweet Counts Per Frequency Problem
Description
LeetCode Problem 1348.
A social media company is trying to monitor activity on their site by analyzing the number of tweets that occur in select periods of time. These periods can be partitioned into smaller time chunks based on a certain frequency (every minute, hour, or day).
For example, the period [10, 10000] (in seconds) would be partitioned into the following time chunks with these frequencies:
- Every minute (60-second chunks):
[10,69],[70,129],[130,189],...,[9970,10000] - Every hour (3600-second chunks):
[10,3609],[3610,7209],[7210,10000] - Every day (86400-second chunks):
[10,10000]
Notice that the last chunk may be shorter than the specified frequency's chunk size and will always end with the end time of the period (10000 in the above example).
Design and implement an API to help the company with their analysis.
Implement the TweetCounts class:
TweetCounts()Initializes theTweetCountsobject.void recordTweet(String tweetName, int time)Stores thetweetNameat the recordedtime(in seconds).List<Integer> getTweetCountsPerFrequency(String freq, String tweetName, int startTime, int endTime)Returns a list of integers representing the number of tweets withtweetNamein each time chunk for the given period of time[startTime, endTime](in seconds) and frequencyfreq.freqis one of"minute","hour", or"day"representing a frequency of every minute, hour, or day respectively.
Example:
Input
["TweetCounts","recordTweet","recordTweet","recordTweet","getTweetCountsPerFrequency","getTweetCountsPerFrequency","recordTweet","getTweetCountsPerFrequency"]
[[],["tweet3",0],["tweet3",60],["tweet3",10],["minute","tweet3",0,59],["minute","tweet3",0,60],["tweet3",120],["hour","tweet3",0,210]]
Output
[null,null,null,null,[2],[2,1],null,[4]]
Explanation
TweetCounts tweetCounts = new TweetCounts();
tweetCounts.recordTweet("tweet3", 0); // New tweet "tweet3" at time 0
tweetCounts.recordTweet("tweet3", 60); // New tweet "tweet3" at time 60
tweetCounts.recordTweet("tweet3", 10); // New tweet "tweet3" at time 10
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 59); // return [2]; chunk [0,59] had 2 tweets
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 60); // return [2,1]; chunk [0,59] had 2 tweets, chunk [60,60] had 1 tweet
tweetCounts.recordTweet("tweet3", 120); // New tweet "tweet3" at time 120
tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 0, 210); // return [4]; chunk [0,210] had 4 tweets
Constraints:
0 <= time, startTime, endTime <= 10^90 <= endTime - startTime <= 10^4- There will be at most
10^4calls in total torecordTweetandgetTweetCountsPerFrequency.
Solution
Approach: Store the Times, Bucket at Query Time
Keep a hash map from each tweet name to the list of times it was recorded. recordTweet just appends the time.
For a query, the chunk size is 60, 3600 or 86400 seconds. The period [startTime, endTime] splits into (endTime - startTime) / size + 1 chunks (integer division), and the chunk of a time t is (t - startTime) / size. So make a list of zeros of that length, go through the times of tweetName, and add one to the chunk of every time that lies in the period. The last chunk may be shorter than size, and the formula handles that automatically because only times up to endTime are counted.
Two details: a period whose chunks are all empty must still return its zeros (and a name that was never recorded gives all zeros), and the same time can be recorded more than once, which counts twice.
There are at most 10^4 calls in total, so scanning one name's times per query is fast enough. With a sorted structure per name (TreeMap or a sorted list), each chunk could instead be counted with two binary searches.
Complexity: recordTweet is O(1). A query is O(T + C), where T is the number of times recorded for that name and C <= 10^4 / 60 + 1 is the number of chunks. Space is O(total tweets).
from collections import defaultdict
class TweetCounts:
SIZE = {"minute": 60, "hour": 3600, "day": 86400}
def __init__(self):
self.times = defaultdict(list) # tweet name -> the times it was recorded
def recordTweet(self, tweetName: str, time: int) -> None:
self.times[tweetName].append(time)
def getTweetCountsPerFrequency(self, freq: str, tweetName: str, startTime: int, endTime: int) -> List[int]:
size = self.SIZE[freq]
counts = [0] * ((endTime - startTime) // size + 1) # one entry per chunk
for t in self.times[tweetName]:
if startTime <= t <= endTime:
counts[(t - startTime) // size] += 1
return counts
# Your TweetCounts object will be instantiated and called as such:
# obj = TweetCounts()
# obj.recordTweet(tweetName,time)
# param_2 = obj.getTweetCountsPerFrequency(freq,tweetName,startTime,endTime)