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 ...
随机推荐
- 关于windows10设置环境变量的问题
在设置环境变量的时候往往在网上能找到这样的文章: 1:新建环境变量 2:将新增的环境变量 加到path 变量中: 3.由于有的小伙伴的 系统是 windows10 在点击 编辑path 环境变量的时候 ...
- 抽象类和虚方法、base关键字
微软官方文档:https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/classes-and-structs/abstract ...
- Python数据分析-Matplotlib图标绘制
Matplotlib介绍 Matplotlib是一个强大的Python绘图和数据可视化的工具包. Matplotlib的主要功能 Matplotlib是python中的一个包,主要用于绘制2D图形(当 ...
- CSLA多语言设置
1.在程序运行文件夹例如“\Bin\Debug\”中包含csla生成的资源文件: 2.在程序运行时,设置CSLA的当前语言为你想要的语言,例如:Csla.Properties.Resources.Cu ...
- 关于tap设备
$QEMU_PATH \ -nographic \ -drive file=./rootfs.ext4,format=raw \ -net nic,vlan=0 -net tap,vlan=0,ifn ...
- bpf 指令集
58 struct bpf_insn { 59 __u8 code; /* opcode */ 60 __u8 dst_reg:4; /* dest register */ 61 __u8 src_r ...
- 银河战舰 [启发式合并+dp]
题面 思路 我们首先考虑传统的链上LIS做法:保存每个长度的LIS末端的最小值,二分查找 那么这道题其实就只是搬到树上来做了而已 我们考虑一个节点,假设它的儿子已经处理完毕了 那么我们选择LIS最长的 ...
- [SDOI2015][bzoj4518] 征途 [斜率优化dp]
题面 传送门 思路 把$vm^2$展开化一下式子,可以得到这样的等价公式: $vm^2=m\sum_{i=1}^m a_i^2-\sum_{i=1}^m a_i$ 那么我们要最小化的就是$\sum_{ ...
- 决策树与随机森林Adaboost算法
一. 决策树 决策树(Decision Tree)及其变种是另一类将输入空间分成不同的区域,每个区域有独立参数的算法.决策树分类算法是一种基于实例的归纳学习方法,它能从给定的无序的训练样本中,提炼出树 ...
- Unity Shader-GodRay,体积光(BillBoard,Volume Shadow,Raidal Blur,Ray-Marching)
好久没有更新博客了,经历了不少事情,好在最近回归了一点正轨,决定继续Unity Shader的学习之路.作为回归的第一篇,来玩一个比较酷炫的效果(当然废话也比较多),一般称之为GodRay(圣光),也 ...