|
| 1 | +/** |
| 2 | + * 1348. Tweet Counts Per Frequency |
| 3 | + * https://leetcode.com/problems/tweet-counts-per-frequency/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * A social media company is trying to monitor activity on their site by analyzing the number |
| 7 | + * of tweets that occur in select periods of time. These periods can be partitioned into |
| 8 | + * smaller time chunks based on a certain frequency (every minute, hour, or day). |
| 9 | + * |
| 10 | + * For example, the period [10, 10000] (in seconds) would be partitioned into the following |
| 11 | + * time chunks with these frequencies: |
| 12 | + * - Every minute (60-second chunks): [10,69], [70,129], [130,189], ..., [9970,10000] |
| 13 | + * - Every hour (3600-second chunks): [10,3609], [3610,7209], [7210,10000] |
| 14 | + * - Every day (86400-second chunks): [10,10000] |
| 15 | + * |
| 16 | + * Notice that the last chunk may be shorter than the specified frequency's chunk size and will |
| 17 | + * always end with the end time of the period (10000 in the above example). |
| 18 | + * |
| 19 | + * Design and implement an API to help the company with their analysis. |
| 20 | + * |
| 21 | + * Implement the TweetCounts class: |
| 22 | + * - TweetCounts() Initializes the TweetCounts object. |
| 23 | + * - void recordTweet(String tweetName, int time) Stores the tweetName at the recorded time |
| 24 | + * (in seconds). |
| 25 | + * - List<Integer> getTweetCountsPerFrequency(String freq, String tweetName, int startTime, |
| 26 | + * int endTime) Returns a list of integers representing the number of tweets with tweetName |
| 27 | + * in each time chunk for the given period of time [startTime, endTime] (in seconds) and |
| 28 | + * frequency freq. |
| 29 | + * - freq is one of "minute", "hour", or "day" representing a frequency of every minute, hour, |
| 30 | + * or day respectively. |
| 31 | + */ |
| 32 | + |
| 33 | +var TweetCounts = function() { |
| 34 | + this.tweets = new Map(); |
| 35 | +}; |
| 36 | + |
| 37 | +/** |
| 38 | + * @param {string} tweetName |
| 39 | + * @param {number} time |
| 40 | + * @return {void} |
| 41 | + */ |
| 42 | +TweetCounts.prototype.recordTweet = function(tweetName, time) { |
| 43 | + if (!this.tweets.has(tweetName)) { |
| 44 | + this.tweets.set(tweetName, []); |
| 45 | + } |
| 46 | + this.tweets.get(tweetName).push(time); |
| 47 | +}; |
| 48 | + |
| 49 | +/** |
| 50 | + * @param {string} freq |
| 51 | + * @param {string} tweetName |
| 52 | + * @param {number} startTime |
| 53 | + * @param {number} endTime |
| 54 | + * @return {number[]} |
| 55 | + */ |
| 56 | +TweetCounts.prototype.getTweetCountsPerFrequency = function(freq, tweetName, startTime, endTime) { |
| 57 | + let interval; |
| 58 | + if (freq === 'minute') interval = 60; |
| 59 | + else if (freq === 'hour') interval = 3600; |
| 60 | + else interval = 86400; |
| 61 | + |
| 62 | + const count = Math.floor((endTime - startTime) / interval) + 1; |
| 63 | + const result = new Array(count).fill(0); |
| 64 | + |
| 65 | + if (this.tweets.has(tweetName)) { |
| 66 | + for (const time of this.tweets.get(tweetName)) { |
| 67 | + if (time >= startTime && time <= endTime) { |
| 68 | + const index = Math.floor((time - startTime) / interval); |
| 69 | + result[index]++; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return result; |
| 75 | +}; |
0 commit comments