forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet-counts-per-frequency.cpp
87 lines (72 loc) · 2.74 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Time: add: O(logn),
// query: O(c), c is the total count of matching records
// Space: O(n)
class TweetCounts {
public:
TweetCounts() {
}
void recordTweet(string tweetName, int time) {
records_[tweetName].emplace(time);
}
vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) {
static const unordered_map<string, int> lookup = {{"minute", 60}, {"hour", 3600}, {"day", 86400}};
const auto& delta = lookup.at(freq);
vector<int> result((endTime - startTime) / delta + 1);
for (auto it = records_[tweetName].lower_bound(startTime);
it != records_[tweetName].end() && *it <= endTime; ++it) {
++result[(*it - startTime) / delta];
}
return result;
}
private:
unordered_map<string, multiset<int>> records_;
};
// Time: add: O(n),
// query: O(rlogn), r is the size of result
// Space: O(n)
class TweetCounts2 {
public:
TweetCounts2() {
}
void recordTweet(string tweetName, int time) {
auto it = lower_bound(records_[tweetName].begin(), records_[tweetName].end(), time);
records_[tweetName].insert(it, time);
}
vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) {
static const unordered_map<string, int> lookup = {{"minute", 60}, {"hour", 3600}, {"day", 86400}};
const auto& delta = lookup.at(freq);
vector<int> result;
for (int i = startTime; i <= endTime; i += delta) {
int j = min(i + delta, endTime + 1);
result.emplace_back(distance(lower_bound(records_[tweetName].cbegin(), records_[tweetName].cend(), i),
lower_bound(records_[tweetName].cbegin(), records_[tweetName].cend(), j)));
}
return result;
}
private:
unordered_map<string, vector<int>> records_;
};
// Time: add: O(1),
// query: O(n)
// Space: O(n)
class TweetCounts3 {
public:
TweetCounts3() {
}
void recordTweet(string tweetName, int time) {
records_[tweetName].emplace_back(time);
}
vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) {
static const unordered_map<string, int> lookup = {{"minute", 60}, {"hour", 3600}, {"day", 86400}};
const auto& delta = lookup.at(freq);
vector<int> result((endTime - startTime) / delta + 1);
for (const auto& t : records_[tweetName]) {
if (startTime <= t && t <= endTime) {
++result[(t - startTime) / delta];
}
}
return result;
}
private:
unordered_map<string, vector<int>> records_;
};