/
LoggerRateLimiter.java
44 lines (37 loc) · 1.33 KB
/
LoggerRateLimiter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class LoggerRateLimiter {
/**
* Question : https://tenderleo.gitbooks.io/leetcode-solutions-/content/GoogleEasy/359.html
*/
public class Logger {
private Map<String, Integer> messageTimestamps;
private final int LIMIT = 10;
/**
* Initialize your data structure here.
*/
public Logger() {
this.messageTimestamps = new HashMap<>();
}
/**
* 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.
*/
public boolean shouldPrintMessage(int timestamp, String message) {
if (!messageTimestamps.containsKey(message)) {
messageTimestamps.put(message, timestamp);
return true;
} else {
if (timestamp - messageTimestamps.get(message) >= LIMIT) {
messageTimestamps.put(message, timestamp);
return true;
}
}
return false;
}
}
/**
* Your Logger object will be instantiated and called as such:
* Logger obj = new Logger();
* boolean param_1 = obj.shouldPrintMessage(timestamp,message);
*/
}