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 ...
随机推荐
- Java 复习计划
前言 打算下学期开学,也就是九月份,去找实习,现在还有三个月时间.(然而还在天天玩 Python..) 定个复习计划. 1. 基础 并发:Java并发编程实战 [ x ] SQL:MySQL,看看书, ...
- zedboard zynq 学习 sobel 边缘检测 IP核 制作 根据 文档 Xapp890
官方文档http://www.xilinx.com/support/documentation/application_notes/xapp890-zynq-sobel-vivado-hls.pdf ...
- Android记事本07
昨天: activity横竖屏切换的生命周期 今天: Anr异常的原因和解决方案 遇到的问题: 无.
- 软工实践 - 第十九次作业 Alpha 冲刺 (10/10)
队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/10046955.htmlz 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 ...
- VS2017 + EF + MySQL 我使用过程中遇到的坑
原文:VS2017 + EF + MySQL 我使用过程中遇到的坑 写在前面: 第一次使用MySQL连接VS的时候本着最新版的应该就是最好的,在MySQL官网下载了最新版的MySQL没有并且安装完成之 ...
- 【DNS】DNS的几个基本概念
一. 根域 就是所谓的“.”,其实我们的网址www.baidu.com在配置当中应该是www.baidu.com.(最后有一点),一般我们在浏览器里输入时会省略后面的点,而这也已经成为了习惯. 根域服 ...
- java实现远程开机
import java.io.IOException; import java.net.*;public class 远程开机 { public static void main(String[] a ...
- [SDOI2015][bzoj3990] 序列 [搜索]
题面 传送门 思路 首先,这道题目有一个非常显然(但是我不会严格证明,只能意会一下)的结论:一个合法的操作序列中,任意两个操作是可以互换的 那么,这个结论加上本题极小的数据范围,为什么不搜索一下呢? ...
- position:absolute和float隐式改变display为inline-block
不论之前是什么类型的元素(display:none除外), 只要设置了position:absolute或float, 都会让元素以display:inline-block的方式显示, 可以设置长宽, ...
- 【ZBH选讲·拍照】
[问题描述] 假设这是一个二次元.LYK召集了n个小伙伴一起来拍照.他们分别有自己的身高Hi和宽度Wi.为了放下这个照片并且每个小伙伴都完整的露出来,必须需要一个宽度为ΣWi,长度为max{Hi}的相 ...