C++ Solution using map


  • 0
    F

    Return true if the message is a new logger message (not present in map) or if the timestamp of new message is higher than the timestamp of same previous message by 10.

    class Logger {
    public:
        map<string, int> mp;
        Logger() {        
        }    
        bool shouldPrintMessage(int timestamp, string message) {
            if (mp.find(message) != mp.end())
            {
               if(abs(mp[message]- timestamp)>=10){
                   mp[message]=timestamp;
                   return true;
                }
               else return false;            
            }
            else{
                mp[message]=timestamp;
                return true;
            }
        }
    };
    

Log in to reply
 

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