C++ Solution - Removes old entries.


  • 0
    D

    My solution is O(n) time and space complexity - this is because I iterate through the map to clear old entries (isn't strictly necessary but what if you have time stamps of higher granularity and millions of possible messages? The question doesn't state that but I figured I'd do that anyway.

    bool shouldPrintMessage(int timestamp, string message) {
        int ageThresh = timestamp - 10;
        vector<string> toErase;
        
        for(auto prevLog : _logs) if(prevLog.second <= ageThresh) toErase.push_back(prevLog.first);
        for(auto erasable : toErase) _logs.erase(erasable);
        
        if(_logs.count(message) == 0)
        {
            _logs[message] = timestamp;
            return true;
        }
        else
        {
            return false;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.