-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy path359-logger-rate-limiter.cpp
More file actions
34 lines (30 loc) · 1013 Bytes
/
359-logger-rate-limiter.cpp
File metadata and controls
34 lines (30 loc) · 1013 Bytes
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
#include <unordered_map>
class Logger {
public:
/** Initialize your data structure here. */
unordered_map<string, int> stream;
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) {
auto it = stream.find(message);
if (it != stream.end()) {
if (timestamp - it->second >= 10) {
it->second = timestamp;
return true;
} else {
return false;
}
} else {
stream[message] = timestamp;
return true;
}
}
};
/**
* Your Logger object will be instantiated and called as such:
* Logger* obj = new Logger();
* bool param_1 = obj->shouldPrintMessage(timestamp,message);
*/