首页 > 其他分享 >[Google] LeetCode 359 Logger Rate Limiter

[Google] LeetCode 359 Logger Rate Limiter

时间:2022-08-28 15:34:39浏览次数:55  
标签:Google timestamp messages Rate printed Limiter rec message Logger

Design a logger system that receives a stream of messages along with their timestamps. Each unique message should only be printed at most every 10 seconds (i.e. a message printed at timestamp t will prevent other identical messages from being printed until timestamp t + 10).

All messages will come in chronological order. Several messages may arrive at the same timestamp.

Implement the Logger class:

  • Logger() Initializes the logger object.
  • bool shouldPrintMessage(int timestamp, string message) Returns true if the message should be printed in the given timestamp, otherwise returns false.

Solution

直接用 \(map\) 记录即可

点击查看代码
class Logger {
private:
    map<string,int> rec;
public:
    Logger() {
        
    }
    
    bool shouldPrintMessage(int timestamp, string message) {
        if(rec.find(message)==rec.end()){
            rec[message]=10+timestamp;
            return true;
        }
        else{
            if(timestamp<rec[message])return false;
            else{
                rec[message]=10+timestamp;
                return true;
            }
        }
    }
};

/**
 * Your Logger object will be instantiated and called as such:
 * Logger* obj = new Logger();
 * bool param_1 = obj->shouldPrintMessage(timestamp,message);
 */

标签:Google,timestamp,messages,Rate,printed,Limiter,rec,message,Logger
From: https://www.cnblogs.com/xinyu04/p/16632840.html

相关文章

  • Educational Codeforces Round 134 (Rated for Div. 2) A-C
    2A,C题wa2不知道为什么。B题少判一个条件:左上角A:题意有点不懂,到最后才知道是有多少种数,就输出这个种数-1即可intn,m;voidsolve(){//cin>>n>>m;chars[......
  • Educational Codeforces Round 134 (Rated for Div. 2)
    EducationalCodeforcesRound134(RatedforDiv.2)D.MaximumAND题目大意给出序列a,b,b可以任意排列,序列c有\(c_i=a_i\bigoplusb_i\)。c序列的价值为c1&c2&c33...&......
  • Flask 学习-19.配置管理flask_sqlalchemy 和 flask_migrate
    前言前面讲了项目中使用config.py可以管理开发、生产、测试等环境的配置,这篇继续学习在项目中添加flask_sqlalchemy和flask_migrate的配置环境准备先pip安装flask_s......
  • Codefores の celebrate!!!
    写在前面作者写这个完全虽写。反正就是乱杀Codefores。难度均为CF*1700~2000。正文CF466CNumberofWays不至于1700实际认为1500。所有数相加就必为3的倍数。只需......
  • Google Web Toolkit (GWT) 说明-基本上没有人用了
    GoogleWebToolkit(GWT)是用于构建和优化复杂的基于浏览器的应用程序的开发工具包。Google的许多产品都使用GWT,包括GoogleAdWords和Orkut。GWT是一个开源的、......
  • Educational Codeforces Round 106 (Rated for Div. 2) | CF1499
    E一个暴力是显然的,\(f(i,j,k)\)表示当前已经使用\(a\)的前\(i\)位,\(b\)的前\(j\)位,最后一位是\(a\)还是\(b\)的。然后\(O(n^2)\)枚举起点跑下去即可。为啥......
  • Flask 学习-15.flask-migrate数据迁移
    前言Flask-SQLAlchemyORM可以直接操作数据库,可以用db.create_all()代码同步表到数据库。当我们需要修改表的字段,比如对表新增字段,修改字段的时候需用到flask-migrate......
  • enumerate() 函数_06
    enumerate()函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标。>>>seasons=['Spring','Summer','Fall','Winter']......
  • python语法随笔:{!r}、*a, = 、enumerate、dic[1]和dic[1.0]、dis、isinstance(True,in
    f"{!r}"等价与'{!r}'.format()输出会带上引号和print('%r'%a)相同a='2'print(f"{a}")print("{}".format(a))print(f"{a!r}")print("{!r}".format(a))输......
  • cooperate
    cooperate[fromLatinco-+operari'towork']collaborate[fromLatincom-+laborare'towork']cooperate的名词是cooperation,不是corporation.Cooperation(w......