Leetcode 359) Logger Rate Limiter
in Algorithms
My Solution
class Logger {
Map<String, Integer> m;
public Logger() {
m = new HashMap<>();
}
public boolean shouldPrintMessage(int timestamp, String message) {
if(m.containsKey(message)){
if(timestamp-m.get(message)<10){
return false;
}
}
m.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);
*/
Other Solution
class Logger {
HashMap<String, Integer> map;
public Logger() {
map = new HashMap<>();
}
public boolean shouldPrintMessage(int timestamp, String message) {
if (!map.containsKey(message)) {
map.put(message, timestamp);
return true;
}
int ts = map.get(message);
if (timestamp < ts+10) return false;
map.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);
*/