Java ConcurrentHashMap solution


  • 5
    L
    import java.util.concurrent.*;
    
    public class Logger {
        ConcurrentHashMap<String, Integer> lastPrintTime;
    
        /** Initialize your data structure here. */
        public Logger() {
            lastPrintTime = new ConcurrentHashMap<String, Integer>();
        }
        
        /** 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) {
    		Integer last = lastPrintTime.get(message);
    
    		return last == null && lastPrintTime.putIfAbsent(message, timestamp) == null
    				|| last != null && timestamp - last >= 10 && lastPrintTime.replace(message, last, timestamp);
    
        }
    }

  • 0
    H

    nice solution but it looks like the size of lastPrintTime will never decrease.


  • 0
    L
    This post is deleted!

Log in to reply
 

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