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 ...
随机推荐
- 详解zabbix2.2.2安装部署(Server端篇)
今天开始安装zabbix.zabbix需要LNMP或者LAMP环境.环境的搭建不在本章范围内. LNMP环境配置 Linux安装:http://www.osyunwei.com/archives/10 ...
- CSS UNIT 详解以及最佳实践
分类 ■ 绝对长度(Absolute units):cm,mm,in,pt,pc 绝对单位之间的换算:1in = 2.54cm=25.4mm=72pt=6pc 绝对长度在css中的表现和其他地方 ...
- eclipse里导入maven项目有红叉的解决办法
导入maven的项目上有红叉,说明eclipse里maven的插件该更新了 1.help里选择install new software 2.点击add,输入name:MavenArchiver, lo ...
- (原)Unreal源码搬山-动画篇 自定义动画节点(一)
@author:黑袍小道 太忙了,来更新下,嘿嘿 前言: 本文是接着上文 Unreal搬山之动画模块_Unreal动画流程和框架,进行简单入门如何自定义动画图标的AnimNode. 正文: 一.Ani ...
- ironic如何支持部署时按需RAID?
新浪大神推荐使用element proliant-tools制作deploy image.element proliant-tools会在ipa ramdisk中安装一个rpm包hpssacli(HP ...
- 【bzoj3809/bzoj3236】Gty的二逼妹子序列/[Ahoi2013]作业 莫队算法+分块
原文地址:http://www.cnblogs.com/GXZlegend/p/6805252.html bzoj3809 题目描述 Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了 ...
- apache+mysql+php实现最大负载的方法
1. 生成静态html页面,squid反向代理,apache,MySQL的负载均衡. 2. 可以采取数据缓存的方法,我们通常在统计数据的时候,需要在原始数据的基础上经过计算等一系列操作,才会得到最终的 ...
- 【BZOJ1179】[Apio2009]Atm (tarjan+SPFA)
显而易见的tarjan+spfa...不解释了 ; type edgetype=record toward,next:longint; end; var edge1,edge2:..maxn] of ...
- Codeforces 585D Lizard Era: Beginning | 折半搜索
参考这个博客 #include<cstdio> #include<algorithm> #include<cstring> #include<map> ...
- 【POJ 2752 Seek the Name, Seek the Fame】
Time Limit: 2000MSMemory Limit: 65536K Description The little cat is so famous, that many couples tr ...