-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0359-LoggerRateLimiter.cs
41 lines (35 loc) · 1.25 KB
/
0359-LoggerRateLimiter.cs
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
//-----------------------------------------------------------------------------
// Runtime: 248ms
// Memory Usage: 43.8 MB
// Link: https://leetcode.com/submissions/detail/328774632/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0359_LoggerRateLimiter
{
private readonly IDictionary<string, int> logHistory;
/** Initialize your data structure here. */
public _0359_LoggerRateLimiter()
{
logHistory = new Dictionary<string, int>();
}
/** 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 bool ShouldPrintMessage(int timestamp, string message)
{
if (!logHistory.ContainsKey(message))
{
logHistory.Add(message, timestamp);
return true;
}
if (logHistory[message] <= timestamp - 10)
{
logHistory[message] = timestamp;
return true;
}
return false;
}
}
}