Skip to content

Commit 8a67b20

Browse files
committedApr 11, 2025
Add solution #1348
1 parent 8bf31e3 commit 8a67b20

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,247 LeetCode solutions in JavaScript
1+
# 1,248 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1024,6 +1024,7 @@
10241024
1345|[Jump Game IV](./solutions/1345-jump-game-iv.js)|Hard|
10251025
1346|[Check If N and Its Double Exist](./solutions/1346-check-if-n-and-its-double-exist.js)|Easy|
10261026
1347|[Minimum Number of Steps to Make Two Strings Anagram](./solutions/1347-minimum-number-of-steps-to-make-two-strings-anagram.js)|Medium|
1027+
1348|[Tweet Counts Per Frequency](./solutions/1348-tweet-counts-per-frequency.js)|Medium|
10271028
1351|[Count Negative Numbers in a Sorted Matrix](./solutions/1351-count-negative-numbers-in-a-sorted-matrix.js)|Easy|
10281029
1352|[Product of the Last K Numbers](./solutions/1352-product-of-the-last-k-numbers.js)|Medium|
10291030
1356|[Sort Integers by The Number of 1 Bits](./solutions/1356-sort-integers-by-the-number-of-1-bits.js)|Easy|
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)
Please sign in to comment.