Leetcode 359: Logger Rate Limiter

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;
 1 public class Logger {
 2     private Dictionary<string, int> cache = new Dictionary<string, int>();
 3
 4     /** Initialize your data structure here. */
 5     public Logger() {
 6
 7     }
 8
 9     /** Returns true if the message should be printed in the given timestamp, otherwise returns false.
10         If this method returns false, the message will not be printed.
11         The timestamp is in seconds granularity. */
12     public bool ShouldPrintMessage(int timestamp, string message) {
13         if (!cache.ContainsKey(message))
14         {
15             cache[message] = timestamp;
16             return true;
17         }
18         else
19         {
20             if (timestamp - cache[message] >= 10)
21             {
22                 cache[message] = timestamp;
23                 return true;
24             }
25             else
26             {
27                 return false;
28             }
29         }
30     }
31 }
32
33 /**
34  * Your Logger object will be instantiated and called as such:
35  * Logger obj = new Logger();
36  * bool param_1 = obj.ShouldPrintMessage(timestamp,message);
37  */

原文地址:https://www.cnblogs.com/liangmou/p/8162874.html

时间: 2024-11-05 19:05:42

Leetcode 359: Logger Rate Limiter的相关文章

359. Logger Rate Limiter

/* * 359. Logger Rate Limiter * 2016-7-14 by Mingyang * 很简单的HashMap,不详谈 */ class Logger { HashMap<String, Integer> map; /** Initialize your data structure here. */ public Logger() { map = new HashMap<String, Integer>(); } /** * Returns true if

[LC] 359. Logger Rate Limiter

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 mes

[LeetCode] Logger Rate Limiter 记录速率限制器

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 mes

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 mes

LeetCode &quot;Logger Rate Limiter&quot;

Queue + hashset class Logger { typedef pair<string, int> Rec; queue<Rec> q; unordered_set<string> hs; public: /** Initialize your data structure here. */ Logger() { } /** Returns true if the message should be printed in the given timesta

Logger Rate Limiter

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 mes

Logger Rate Limiter 十秒限时计数器

[抄题]: [暴力解法]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: 没有考虑到更新的情况:0T-8F-11T-14F [二刷]: [三刷]: [四刷]: [五刷]: [五分钟肉眼debug的结果]: [总结]: [复杂度]:Time complexity: O() Space complexity: O(

RocksDB Rate Limiter源码解析

这次的项目我们重点关注RocksDB中的一个环节:Rate Limiter.其实Rate Limiter的思想在很多其他系统中也很常用. 在RocksDB中,后台会实时运行compaction和flush操作,这些都会对磁盘进行大量的写操作.可以通过Rate Limiter来控制最大写入速度的上限.因为在某些场景下,突发的大量写入会导致很大的read latency,从而影响系统性能. Rate Limiter的基本原理是令牌桶算法:系统每秒往桶里丢数量为1/QPS的令牌(满了为止),写请求只有

Leetcode 前 400 重点 250 题

这个重点题目是把Leetcode前400题进行精简,划分精简规则如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & 389,保留一个) 所有题目尽量保证客观公正,只是按大概率删除不常考题目,很多题目面经出现过, 但出现次数属于个位数或者只有一两家出现进行删除.所以如在面试中出现删除题目概不负责,这只是从概率上删除低频,简单题目. 旨在减轻大家的刷