Posted on

Description

Submission

class TweetCounts {
    unordered_map<string, set<int>> records;
    unordered_map<string, int> freqMap;
public:
    TweetCounts() {
        freqMap = {{"minute", 60}, {"hour", 3600}, {"day", 86400}};
    }
    
    void recordTweet(string tweetName, int time) {
        records[tweetName].insert(time);
    }
    
    vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) {
        int delta = freqMap[freq];
        
        auto& Set = records[tweetName];
        
        
        int t0 = startTime, t1 = min(endTime+1, t0 + delta);
        auto iter = Set.begin();
        vector<int> rets;
        for(; true; t0 = t1, t1 = min(endTime+1, t0 + delta)) {
            
            int cnt = 0;
            while(iter != Set.end()) {
                if(*iter < t1 && *iter >= t0) {
                    cnt++;
                    iter++;
                } else if(*iter >= t1) {
                    break;
                } else { // *iter < t0;
                    iter++;
                }
            }
            rets.push_back(cnt);
            
            if(t1 >= endTime + 1) break;
        }
        
       
        return rets;
    }
};

/**
 * 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);
 */

Leave a Reply

Your email address will not be published. Required fields are marked *