A Java Solution


  • 11
     public class Logger {
    HashMap<String,Integer> map;
    /** Initialize your data structure here. */
    public Logger() {
        map=new HashMap<>();
    }
    
    /** Returns true if the message should be printed in the given timestamp, otherwise returns false. The timestamp is in seconds granularity. */
    public boolean shouldPrintMessage(int timestamp, String message) {
    //update timestamp of the message if the message is coming in for the first time,or the last coming time is earlier than 10 seconds from now
        if(!map.containsKey(message)||timestamp-map.get(message)>=10){
            map.put(message,timestamp);
            return true;
        }
        return false;
    }
    

    }


  • 0
    W
    This post is deleted!

  • 1
    S

    The problem with this approach is that your map size will keep growing. It will have messages that have come since the beginning even though we need to keep only the words that have come only 10 seconds before the current timestamp.


Log in to reply
 

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