-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtweetCountsPerFrequency.cpp
More file actions
49 lines (42 loc) · 1.28 KB
/
tweetCountsPerFrequency.cpp
File metadata and controls
49 lines (42 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Source: https://leetcode.com/problems/tweet-counts-per-frequency/
// Author: Miao Zhang
// Date: 2021-04-26
class TweetCounts {
public:
TweetCounts() {
}
void recordTweet(string tweetName, int time) {
record[tweetName][time]++;
}
vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) {
int f = 1;
if (freq == "minute") {
f *= 60;
} else if (freq == "hour") {
f *= 60 * 60;
} else {
f *= 60 * 60 * 24;
}
vector<int> res;
int t = startTime;
while (t <= endTime) {
auto begin = record[tweetName].lower_bound(t);
auto end = record[tweetName].upper_bound(min(t + f - 1, endTime));
int cnt = 0;
for (auto it = begin; it != end; it++) {
cnt += it->second;
}
res.push_back(cnt);
t += f;
}
return res;
}
private:
unordered_map<string, map<int, int>> record;
};
/**
* 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);
*/