Neat Javascript solution that beats 100% of submissions


  • 1
    D
    var Logger = function() {
        this.messages = {};
        this.TIME_LIMIT = 10;
    };
    
    /**
     * @param {number} timestamp 
     * @param {string} message
     * @return {boolean}
     */
    Logger.prototype.shouldPrintMessage = function(timestamp, message) {
        if ( !this.messages.hasOwnProperty(message) ) {
            // add to map & return true
            this.messages[message] = timestamp;
            return true;
        }
            
        if (
            this.messages.hasOwnProperty(message) && 
            timestamp - this.messages[message] >= this.TIME_LIMIT
        ) {
            // update map & return true
            this.messages[message] = timestamp;
            return true;
        }
        
        return false;
    };
    

Log in to reply
 

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