classTweetCounts { unordered_map<string, map<int, int>> string_map; public: TweetCounts() { } voidrecordTweet(string tweetName, int time){ ++string_map[tweetName][time]; } vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime){ int interval = 0; if (freq == "hour") { interval = 3600; } elseif (freq == "day") { interval = 3600 * 24; } else { interval = 60; } constauto& m = string_map[tweetName]; vector<int> ans; int st = startTime; auto start_it = m.lower_bound(st); auto end_it = m.upper_bound(endTime); auto it = start_it; while (st <= endTime) { auto next_it = m.lower_bound(st + interval); int accu = 0; for (; it != next_it && it != end_it; ++it) { accu += it->second; } ans.push_back(accu); st += interval; } return ans; } };
/** * 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); */