-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLogger.cpp
37 lines (32 loc) · 970 Bytes
/
Logger.cpp
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
// Problem: https://leetcode.com/problems/logger-rate-limiter/
#include<unordered_map>
using namespace std;
/**
* Your Logger object will be instantiated and called as such:
* Logger* obj = new Logger();
* bool param_1 = obj->shouldPrintMessage(timestamp,message);
*/
class Logger {
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) {
if (this->_tstamp.find(message) != this->_tstamp.end()) {
int old_tstamp = this->_tstamp[message];
if (timestamp - old_tstamp >= 10) {
this->_tstamp[message] = timestamp;
return true;
}
return false;
}
this->_tstamp[message] = timestamp;
return true;
}
private:
unordered_map<string, int> _tstamp;
};