zoukankan      html  css  js  c++  java
  • Logger Rate Limiter -- LeetCode

    Design a logger system that receive stream of messages along with its timestamps, each message should be printed if and only if it is not printed in the last 10 seconds.

    Given a message and a timestamp (in seconds granularity), return true if the message should be printed in the given timestamp, otherwise returns false.

    It is possible that several messages arrive roughly at the same time.

    Example:

    Logger logger = new Logger();
    
    // logging string "foo" at timestamp 1
    logger.shouldPrintMessage(1, "foo"); returns true; 
    
    // logging string "bar" at timestamp 2
    logger.shouldPrintMessage(2,"bar"); returns true;
    
    // logging string "foo" at timestamp 3
    logger.shouldPrintMessage(3,"foo"); returns false;
    
    // logging string "bar" at timestamp 8
    logger.shouldPrintMessage(8,"bar"); returns false;
    
    // logging string "foo" at timestamp 10
    logger.shouldPrintMessage(10,"foo"); returns false;
    
    // logging string "foo" at timestamp 11
    logger.shouldPrintMessage(11,"foo"); returns true;

    思路:用队列来记录当前的所有消息,用一个set来记录哪些消息在这个队列中。

     1 class Message {
     2 public:
     3     int timestamp;
     4     string msg;
     5     Message(int ts, string m) : timestamp(ts), msg(m) {}
     6 };
     7 class Logger {
     8 public:
     9     /** Initialize your data structure here. */
    10     queue<Message> logQueue;
    11     unordered_set<string> msgInQueue;
    12     Logger() {
    13         
    14     }
    15     
    16     /** Returns true if the message should be printed in the given timestamp, otherwise returns false.
    17         If this method returns false, the message will not be printed.
    18         The timestamp is in seconds granularity. */
    19     bool shouldPrintMessage(int timestamp, string message) {
    20         //remove all msgs before 10s ago
    21         while (!logQueue.empty() && logQueue.front().timestamp + 10 <= timestamp) {
    22             msgInQueue.erase(logQueue.front().msg);
    23             logQueue.pop();
    24         }
    25         bool notInQueue = msgInQueue.count(message) == 0;
    26         //this msg will be printed, add it to log
    27         if (notInQueue) {
    28             logQueue.push(Message(timestamp, message));
    29             msgInQueue.insert(message);
    30         }
    31         return notInQueue;
    32     }
    33 };
    34 
    35 /**
    36  * Your Logger object will be instantiated and called as such:
    37  * Logger obj = new Logger();
    38  * bool param_1 = obj.shouldPrintMessage(timestamp,message);
    39  */
  • 相关阅读:
    Java中使用Base64编码URL
    JSON Web Token (JWT)入门学习
    1047. 删除字符串中的所有相邻重复项
    1021. 删除最外层的括号
    使用shell获取随机端口<帮你解决端口的占用烦恼>
    初始化一个vue项目并生成完整的目录结构
    mysql-常用字符函数
    设计模式-单例模式-饿汉和懒汉
    Java-指令的重排序
    Java-反射类加载到内存分析
  • 原文地址:https://www.cnblogs.com/fenshen371/p/5771535.html
Copyright © 2011-2022 走看看