Design a logger system this receive stream of messages along with its timestamps, each message should be printedifand onlyifIt isn't printed in the last 10seconds. Given a message and a timestamp (in seconds granularity),return true ifThe message should is printed in the given timestamp, otherwise returnsfalse. It is possible this several messages arrive roughly at the same time. Example:logger Logger=NewLogger ();//Logging string "foo" at timestamp 1Logger.shouldprintmessage (1, "foo"); Returnstrue; //Logging string "Bar" at timestamp 2Logger.shouldprintmessage (2, "bar"); Returnstrue;//Logging string "foo" at timestamp 3Logger.shouldprintmessage (3, "foo"); Returnsfalse;//Logging string "Bar" at timestamp 8Logger.shouldprintmessage (8, "bar"); Returnsfalse;//Logging string "foo" at timestampLogger.shouldprintmessage (Ten, "Foo"); Returnsfalse;//Logging string "foo" at timestampLogger.shouldprintmessage (One, "foo"); Returnstrue;
HashMap
1 Public classLogger {2Hashmap<string, integer>map;3 4 /**Initialize your data structure here.*/5 PublicLogger () {6Map =NewHashmap<string, integer>();7 }8 9 /**Returns True if the message should is printed in the given timestamp, otherwise Returns false.Ten If This method returns false, the message won't be printed. One The timestamp is in seconds granularity.*/ A Public BooleanShouldprintmessage (inttimestamp, String message) { - if(!map.containskey (message) | | | timestamp >=map.get (message)) { -Map.put (Message, timestamp+10); the return true; - } - return false; - } + } - + /** A * Your Logger object would be instantiated and called as such: at * Logger obj = new Logger (); - * Boolean param_1 = Obj.shouldprintmessage (timestamp,message); - */
Leetcode:logger Rate Limiter