Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1348.html

1348. Tweet Counts Per Frequency (Medium)

Implement the class TweetCounts that supports two methods:

1. recordTweet(string tweetName, int time)

  • Stores the tweetName at the recorded time (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 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.

 

Constraints:

  • There will be at most 10000 operations considering both recordTweet and getTweetCountsPerFrequency.
  • 0 <= time, startTime, endTime <= 10^9
  • 0 <= endTime - startTime <= 10^4

Related Topics:
Design

Solution 1.

  • class TweetCounts {
        private Map<String, TreeMap<Integer, Integer>> data = new HashMap<>();
    
        public TweetCounts() {
        }
    
        public void recordTweet(String tweetName, int time) {
            data.putIfAbsent(tweetName, new TreeMap<>());
            var tm = data.get(tweetName);
            tm.put(time, tm.getOrDefault(time, 0) + 1);
        }
    
        public List<Integer> getTweetCountsPerFrequency(
            String freq, String tweetName, int startTime, int endTime) {
            int f = 60;
            if ("hour".equals(freq)) {
                f = 3600;
            } else if ("day".equals(freq)) {
                f = 86400;
            }
            var tm = data.get(tweetName);
            List<Integer> ans = new ArrayList<>();
            for (int i = startTime; i <= endTime; i += f) {
                int s = 0;
                int end = Math.min(i + f, endTime + 1);
                for (int v : tm.subMap(i, end).values()) {
                    s += v;
                }
                ans.add(s);
            }
            return ans;
        }
    }
    
    /**
     * 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);
     */
    
  • class TweetCounts {
    public:
        TweetCounts() {
        }
    
        void recordTweet(string tweetName, int time) {
            data[tweetName].insert(time);
        }
    
        vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) {
            int f = 60;
            if (freq == "hour")
                f = 3600;
            else if (freq == "day")
                f = 86400;
            vector<int> ans((endTime - startTime) / f + 1);
            auto l = data[tweetName].lower_bound(startTime);
            auto r = data[tweetName].upper_bound(endTime);
            for (; l != r; ++l) {
                ++ans[(*l - startTime) / f];
            }
            return ans;
        }
    
    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);
     */
    
  • from sortedcontainers import SortedList
    
    
    class TweetCounts:
        def __init__(self):
            self.d = {"minute": 60, "hour": 3600, "day": 86400}
            self.data = defaultdict(SortedList)
    
        def recordTweet(self, tweetName: str, time: int) -> None:
            self.data[tweetName].add(time)
    
        def getTweetCountsPerFrequency(
            self, freq: str, tweetName: str, startTime: int, endTime: int
        ) -> List[int]:
            f = self.d[freq]
            tweets = self.data[tweetName]
            t = startTime
            ans = []
            while t <= endTime:
                l = tweets.bisect_left(t)
                r = tweets.bisect_left(min(t + f, endTime + 1))
                ans.append(r - l)
                t += f
            return ans
    
    
    # Your TweetCounts object will be instantiated and called as such:
    # obj = TweetCounts()
    # obj.recordTweet(tweetName,time)
    # param_2 = obj.getTweetCountsPerFrequency(freq,tweetName,startTime,endTime)
    
    

All Problems

All Solutions