forked from drjkuo/leetcode-javascript-python3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path359.LoggerRateLimiter.cpp
More file actions
43 lines (35 loc) · 1.11 KB
/
359.LoggerRateLimiter.cpp
File metadata and controls
43 lines (35 loc) · 1.11 KB
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
class Logger {
private: std::unordered_map<string, int> mapLogger;
public:
/** Initialize your data structure here. */
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) {
std::unordered_map<string, int>::iterator iter = mapLogger.find(message);
if(iter != mapLogger.end()) // found
{
if (iter->second <= (timestamp-10)) // and not in 10 seconds
{
mapLogger[iter->first] = timestamp; // update
return true;
}
else
{
return false;
}
}
else // not found
{
mapLogger[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);
*/