DEV Community

loading...
Cover image for Solve Leetcode Problems and Get Offers From Your Dream Companies||Tweets count per frequency

Solve Leetcode Problems and Get Offers From Your Dream Companies||Tweets count per frequency

nilmadhabmondal profile image Nil Madhab Originally published at simplecoding.dev ・Updated on ・3 min read

Problem 1348. Tweet Counts Per Frequency.
Alt Text

In this series, I am going to solve Leetcode medium problems live with my friend, which you can see on our youtube channel, Today we will do Problem Problem 1348. Tweet Counts Per Frequency.

A little bit about me, I have offers from Uber India and Amazon India in the past, and I am currently working for Booking.com in Amsterdam.

Problem Statement

Implement the class TweetCounts that supports two methods:

  • recordTweet(string tweetName, int time)

    • Stores the tweetName at the recorded time (in seconds).
  • 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 on freq) starting from the startTime (in seconds) and ending at the endTime (in seconds).
    • freq is always minute, hour or day, representing the time interval to get the total number of occurrences for the given tweetName.
    • 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 number i and delta (which depends on freq).

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);
tweetCounts.recordTweet("tweet3", 60);
tweetCounts.recordTweet("tweet3", 10); // All tweets correspond to "tweet3" with recorded times at 0, 10 and 60.
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 59); // return [2]. The frequency is per minute (60 seconds), so there is one interval of time: 1) [0, 60> - > 2 tweets.
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 60); // return [2, 1]. The frequency is per minute (60 seconds), so there are two intervals of time: 1) [0, 60> - > 2 tweets, and 2) [60,61> - > 1 tweet.
tweetCounts.recordTweet("tweet3", 120); // All tweets correspond to "tweet3" with recorded times at 0, 10, 60 and 120.
tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 0, 210); // return [4]. The frequency is per hour (3600 seconds), so there is one interval of time: 1) [0, 211> - > 4 tweets.

Youtube Discussion

Solution

In this problem, we have to implement two methods. On is recordTweet which stores the tweet along with its timestamp and another one is getTweetsPerFrequenecy where we have to return no of tweets in some given frequency (minute, hour, day).

Here we will be using a HashMap to store the tweet data. The key is the title of the tweet and the value is a list which contains all the timestamps where this tweet is published.

In the recordTweet method, we store the tweet into the hashmap. This method runs in O(1) time complexity as the retrieval of the list from hashmap takes constant time.

The input frequency can be minute, hour and day. All the inputs are given in seconds. Therefore to calculate the frequency in a minute, hour and day terms we need to use different constants.

minute = 60 seconds

hour = 60 minutes = 3600 seconds

day = 24 hours = 24*3600 seconds

The getFreq method is used to calculate the occurrences of the tweet in different timeframes. We can visualize the problem as placing the tweets into different buckets. The bucket size depends on the frequency (minute, hour, day). We will find the unit timeframes in seconds depending upon the frequency.

eg : Start Time = 135 , End Time = 345 , freq = minutes

The bucket size will be 135-194 | 195-254 | 255-314 | 315-344

So we will place the tweets with different timestamps into these buckets. The index is found out by using the following formula :

index = (timestamp — startTime)/delta ; delta depends on the frequency

The final list is return in the method.

The java code along with a dry run of the example is given below. Also, the code is properly documented.

The code for this problem can be found in the following repository.

Thank You for reading and Follow this publication for more LeetCode problems!😃
Leetcode simplified

Sign up for Leetcode Simplified
By LeetCode Simplified

Get latest leetcode solution with youtube breakdown Take a look
You're an editor of Leetcode Simplified

Discussion (0)

pic
Editor guide