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(, "foo"); returns true; // logging string "bar" at timestamp 2
logger.shouldPrintMessage(,"bar"); returns true; // logging string "foo" at timestamp 3
logger.shouldPrintMessage(,"foo"); returns false; // logging string "bar" at timestamp 8
logger.shouldPrintMessage(,"bar"); returns false; // logging string "foo" at timestamp 10
logger.shouldPrintMessage(,"foo"); returns false; // logging string "foo" at timestamp 11
logger.shouldPrintMessage(,"foo"); returns true;
思路:用队列来记录当前的所有消息,用一个set来记录哪些消息在这个队列中。
class Message {
public:
int timestamp;
string msg;
Message(int ts, string m) : timestamp(ts), msg(m) {}
};
class Logger {
public:
/** Initialize your data structure here. */
queue<Message> logQueue;
unordered_set<string> msgInQueue;
Logger() { } /** Returns true if the message should be printed in the given timestamp, otherwise returns false.
If this method returns false, the message will not be printed.
The timestamp is in seconds granularity. */
bool shouldPrintMessage(int timestamp, string message) {
//remove all msgs before 10s ago
while (!logQueue.empty() && logQueue.front().timestamp + <= timestamp) {
msgInQueue.erase(logQueue.front().msg);
logQueue.pop();
}
bool notInQueue = msgInQueue.count(message) == ;
//this msg will be printed, add it to log
if (notInQueue) {
logQueue.push(Message(timestamp, message));
msgInQueue.insert(message);
}
return notInQueue;
}
}; /**
* Your Logger object will be instantiated and called as such:
* Logger obj = new Logger();
* bool param_1 = obj.shouldPrintMessage(timestamp,message);
*/
Logger Rate Limiter -- LeetCode的更多相关文章
- 359. Logger Rate Limiter
/* * 359. Logger Rate Limiter * 2016-7-14 by Mingyang * 很简单的HashMap,不详谈 */ class Logger { HashMap< ...
- [LeetCode] Logger Rate Limiter 记录速率限制器
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- LeetCode Logger Rate Limiter
原题链接在这里:https://leetcode.com/problems/logger-rate-limiter/ 题目: Design a logger system that receive s ...
- LeetCode 359. Logger Rate Limiter (记录速率限制器)$
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- [LeetCode] 359. Logger Rate Limiter 记录速率限制器
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
- LeetCode 359 Logger Rate Limiter
Problem: Design a logger system that receive stream of messages along with its timestamps, each mess ...
- 【LeetCode】359. Logger Rate Limiter 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- Logger Rate Limiter 十秒限时计数器
[抄题]: Design a logger system that receive stream of messages along with its timestamps, each message ...
- Logger Rate Limiter
Design a logger system that receive stream of messages along with its timestamps, each message shoul ...
随机推荐
- python3 打印九九乘法口诀表
for i in range(1, 10): for j in range(1, i+1): # print(f'{i}×{j}={i*j}', end='\t') print('%d×%d=%d' ...
- 孤荷凌寒自学python第三十七天python的文件与内存变量之间的序列化与反序列化
孤荷凌寒自学python第三十七天python的文件与内存变量之间的序列化与反序列化 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.什么是序列化与反序列化 序列化是指将内存中的数据进行指 ...
- Jtag To Axi4 debug 读写寄存器的tcl脚本封装
把下列代码保存为.tcl或者.txt文本保存在某个路径下 打开vivado,在tcl concle中输入 “source 文件路径”,将脚本加载至工具中后, 例如读寄存器地址32'h12345678的 ...
- Elasticsearch同义词词汇单元过滤器
1 简单扩展 "jump,hop,leap" 搜索jump会检索出包含jump.hop或leap的词 1.1 扩展应用在索引阶段 1.2 扩展应用在查询阶段 1.3 对比 2 简单 ...
- shell之ip命令
转:出处我也不知道了,学习时候记下的笔记 1.作用 ip是iproute2软件包里面的一个强大的网络配置工具,它能够替代一些传统的网络管理工具,例如ifconfig.route等,使用权限为超级用户. ...
- JSP/Servlet Web 学习笔记 DayFive
ServletConfig <只对当前Servlet有效> (1)在Web容器初始化Servlet实例时,都会为这个Servlet准备一个唯一的ServletConfig实例(俗称Serv ...
- kafka+windows+java+springboot中的配置
1.百度kafka+zookeeper+windows配置 1.1 zookeeper配置 dataDir=/tmp/zookeeper # the port at which the client ...
- Android程序猿必须警示的13个坑
Android开发中,犯错是难免的,不犯错是不正常的,但是犯了错以后,我们必须时刻谨记这些坑,避免再次被坑,下面小编整理了13个,日常工作中,比较常见且易犯的错误,分享给大家. 1.类的 ...
- Codeforces Round #386 (Div. 2) 746F(set的运用)
题目大意 给出一个歌单(有n首歌),每个歌都有愉悦值和时间,你可以选择从第x首歌开始听(也就是选择连续的一段),并且你可以选择w首歌让它的时间减半,限制时间为k,求最大的愉悦值 首先我们需要贪心一下, ...
- [Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) ](A~E)
A: 题目大意:给你$a,b,c$三条边,可以给任意的边加任意的长度,求最少共加多少长度使得可以构成三角形 题解:排个序,若可以组成,输出$0$,否则输出$c-a-b+1(设a\leqslant b\ ...