[LeetCode] 359. Logger Rate Limiter

Design a logger system that receives a stream of messages along with their timestamps. Each unique message should only be printed at most every 10 seconds (i.e. a message printed at timestamp t will prevent other identical messages from being printed until timestamp t + 10).

All messages will come in chronological order. Several messages may arrive at the same timestamp.

Implement the Logger class:

  • Logger() Initializes the logger object.
  • bool shouldPrintMessage(int timestamp, string message) Returns true if the message should be printed in the given timestamp, otherwise returns false.

Example 1:

Input
["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"]
[[], [1, "foo"], [2, "bar"], [3, "foo"], [8, "bar"], [10, "foo"], [11, "foo"]]
Output
[null, true, true, false, false, false, true]

Explanation
Logger logger = new Logger();
logger.shouldPrintMessage(1, "foo");  // return true, next allowed timestamp for "foo" is 1 + 10 = 11
logger.shouldPrintMessage(2, "bar");  // return true, next allowed timestamp for "bar" is 2 + 10 = 12
logger.shouldPrintMessage(3, "foo");  // 3 < 11, return false
logger.shouldPrintMessage(8, "bar");  // 8 < 12, return false
logger.shouldPrintMessage(10, "foo"); // 10 < 11, return false
logger.shouldPrintMessage(11, "foo"); // 11 >= 11, return true, next allowed timestamp for "foo" is 11 + 10 = 21

Constraints:

  • 0 <= timestamp <= 109
  • Every timestamp will be passed in non-decreasing order (chronological order).
  • 1 <= message.length <= 30
  • At most 104 calls will be made to shouldPrintMessage.

日志速率限制器。

请你设计一个日志系统,可以流式接收消息以及它的时间戳。每条 不重复 的消息最多只能每 10 秒打印一次。也就是说,如果在时间戳 t 打印某条消息,那么相同内容的消息直到时间戳变为 t + 10 之前都不会被打印。

所有消息都按时间顺序发送。多条消息可能到达同一时间戳。

实现 Logger 类:

Logger() 初始化 logger 对象
bool shouldPrintMessage(int timestamp, string message) 如果这条消息 message 在给定的时间戳 timestamp 应该被打印出来,则返回 true ,否则请返回 false 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/logger-rate-limiter
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是用hashmap记录日志内容和时间,每次看到有相同日志进来,则查看一下上一个 timestamp 是否在10秒钟以内。

Java实现

 1 class Logger {
 2     HashMap<String, Integer> map;
 3     int time;
 4     /** Initialize your data structure here. */
 5     public Logger() {
 6         map = new HashMap<>();
 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 boolean shouldPrintMessage(int timestamp, String message) {
13         if (map.containsKey(message)) {
14             time = map.get(message);
15             if (Math.abs(timestamp - time) < 10) return false;
16         }
17         map.put(message, timestamp);
18         return true;
19     }
20 }
21 
22 /**
23  * Your Logger object will be instantiated and called as such:
24  * Logger obj = new Logger();
25  * boolean param_1 = obj.shouldPrintMessage(timestamp,message);
26  */

 

JavaScript实现

 1 /**
 2  * Initialize your data structure here.
 3  */
 4 var Logger = function() {
 5     this.map = new Map();
 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  * @param {number} timestamp 
13  * @param {string} message
14  * @return {boolean}
15  */
16 Logger.prototype.shouldPrintMessage = function(timestamp, message) {
17     return (this.map.has(message) && timestamp - this.map.get(message) < 10) ? false : this.map.set(message, timestamp);
18 };
19 
20 /** 
21  * Your Logger object will be instantiated and called as such:
22  * var obj = new Logger()
23  * var param_1 = obj.shouldPrintMessage(timestamp,message)
24  */

 

LeetCode 题目总结

posted @ 2020-04-14 02:01  CNoodle  阅读(186)  评论(0)    收藏  举报