forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tweet Counts Per Frequency.cpp
41 lines (32 loc) · 1.15 KB
/
Tweet Counts Per Frequency.cpp
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
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
//less_equal to use it as a multiset
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
class TweetCounts {
public:
unordered_map<string, pbds> tweet;
TweetCounts() {
tweet.clear();
}
void recordTweet(string tweetName, int time) {
tweet[tweetName].insert(time);
}
vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) {
int interval;
if(freq == "minute") interval = 60;
else if(freq == "hour") interval = 3600;
else interval = 86400;
vector<int> ans;
for(int i=startTime; i<=endTime; i+=interval)
{
int start = i, end = min(endTime, i+interval-1);
//[start - end] or [start - end+1)
auto low = tweet[tweetName].order_of_key(start);
auto high = tweet[tweetName].order_of_key(end+1);
ans.push_back(high-low);
}
return ans;
}
};