-
Notifications
You must be signed in to change notification settings - Fork 370
/
Copy paths1.cpp
26 lines (24 loc) · 868 Bytes
/
s1.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
// OJ: https://leetcode.com/problems/tweet-counts-per-frequency/
// Author: github.com/lzl124631x
// Time:
// recordTweet: O(logN)
// getTweetCountsPerFrequency: O(N)
// Space: O(N)
class TweetCounts {
unordered_map<string, multiset<int>> m;
public:
TweetCounts() {}
void recordTweet(string tweetName, int time) {
m[tweetName].insert(time);
}
vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) {
int d = freq[0] == 'm' ? 60 : (freq[0] == 'h' ? 3600 : (3600 * 24));
vector<int> ans((endTime - startTime) / d + 1);
if (!m.count(tweetName)) return ans;
auto &ps = m[tweetName];
for (auto t = ps.lower_bound(startTime); t != ps.end() && *t <= endTime; ++t) {
++ans[(*t - startTime) / d];
}
return ans;
}
};