Leetcode 1348: Push message count (super detailed solution !!!)

You can support the realization of a tweet count class the following two methods TweetCounts:

1.recordTweet(string tweetName, int time)

  • Tweets posted record: The user tweetNamein time(in seconds units) posted a time tweets.

2.getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime)

  • Return from the start time startTime(to the second units) to the end of time endTime(in seconds units) within each sub- * minute , * when * hour * or day Day * * (depending on freq) the user specified tweetNamethe total number of tweets posted.
  • freqThe value is always divided * minute , * when the hour or day day one, retrieves the specified user tweetNametweet the number of time intervals.
  • The first time interval is always from the startTimebeginning, so the time interval [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)>in which i, and delta(depending on freq) are non-negative integers.

Example:

输入:
["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]]

输出:
[null,null,null,null,[2],[2,1],null,[4]]

解释:
TweetCounts tweetCounts = new TweetCounts();
tweetCounts.recordTweet("tweet3", 0);
tweetCounts.recordTweet("tweet3", 60);
tweetCounts.recordTweet("tweet3", 10);                             // "tweet3" 发布推文的时间分别是 0, 10 和 60 。
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 59); // 返回 [2]。统计频率是每分钟(60 秒),因此只有一个有效时间间隔 [0,60> - > 2 条推文。
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 60); // 返回 [2,1]。统计频率是每分钟(60 秒),因此有两个有效时间间隔 1) [0,60> - > 2 条推文,和 2) [60,61> - > 1 条推文。 
tweetCounts.recordTweet("tweet3", 120);                            // "tweet3" 发布推文的时间分别是 0, 10, 60 和 120 。
tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 0, 210);  // 返回 [4]。统计频率是每小时(3600 秒),因此只有一个有效时间间隔 [0,211> - > 4 条推文。

prompt:

  • Taking into account recordTweetand getTweetCountsPerFrequency, with up 10000operations.
  • 0 <= time, startTime, endTime <= 10^9
  • 0 <= endTime - startTime <= 10^4

Problem-solving ideas

Title relatively long, but relatively easy to operate, through mapan array of records, and when the data insertion can be sorted or unsorted selected, the query time by half (if not in front of the sort, it is necessary to sort).

class TweetCounts:

    def __init__(self):
        self.tweets = collections.defaultdict(list)
        

    def recordTweet(self, tweetName: str, time: int) -> None:
        bisect.insort_left(self.tweets[tweetName], time)
        

    def getTweetCountsPerFrequency(self, freq: str, tweetName: str, startTime: int, endTime: int) -> List[int]:
        delta = 1
        if freq == 'minute':
            delta = 60
        elif freq == 'hour':
            delta = 60 * 60
        elif freq == 'day':
            delta = 60 * 60 * 24
        
        res = [0] * ((endTime - startTime) // delta + 1)
        
        pos = bisect.bisect_left(self.tweets[tweetName], startTime)
        while pos < len(self.tweets[tweetName]) and self.tweets[tweetName][pos] <= endTime:
            res[(self.tweets[tweetName][pos] - startTime) // delta] += 1
            pos += 1
        return res

Other language versions of the questions I added to my GitHub Leetcode

If you have questions, I wish to point out! ! !

Published 731 original articles · won praise 457 · views 830 000 +

Guess you like

Origin blog.csdn.net/qq_17550379/article/details/104258970