359. Logger Rate Limiter

T: O(1)

S: O(n), hashmap size

class Logger {

    Map<String, Integer> logger;
    public Logger() {
        logger = new HashMap<>();
    }
    
    public boolean shouldPrintMessage(int timestamp, String message) {
        if (logger.containsKey(message)) {
            int prevTime = logger.get(message);
            if (prevTime + 10 > timestamp) {
                return false;
            }
        }
        logger.put(message, timestamp);
        return true;
    }
}

/**
 * Your Logger object will be instantiated and called as such:
 * Logger obj = new Logger();
 * boolean param_1 = obj.shouldPrintMessage(timestamp,message);
 
 
 t
 unique msg can't show
 t+10
 
 Several messages may arrive at the same timestamp.  (hashmap), <String, time>
 
 [[], [1, "foo"], [2, "bar"], [3, "foo"], [8, "bar"], [10, "foo"], [11, "foo"]]
 [null, true,         true,      false,      false,      false,       true]
 
 
 time is ordered
 
 T: O(1)
 S: O(n), hashmap size
 */

Last updated