-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
40 lines (38 loc) · 1.21 KB
/
Program.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
using System;
using System.Collections.Generic;
namespace LoggerRateLimiter
{
class Program
{
static void Main(string[] args)
{
var logger = new Logger();
Console.WriteLine(logger.ShouldPrintMessage(2, "bar"));
Console.WriteLine(logger.ShouldPrintMessage(3, "foo"));
Console.WriteLine(logger.ShouldPrintMessage(8, "bar"));
Console.WriteLine(logger.ShouldPrintMessage(10, "foo"));
Console.WriteLine(logger.ShouldPrintMessage(11, "foo"));
}
}
public class Logger
{
private Dictionary<int, HashSet<string>> logger;
public Logger()
{
logger = new Dictionary<int, HashSet<string>>();
}
public bool ShouldPrintMessage(int timestamp, string message)
{
if(!logger.ContainsKey(timestamp))
logger[timestamp] = new HashSet<string>();
for (int i = 0; i < 10; i++)
{
int time = timestamp - i;
if (logger.ContainsKey(time) && logger[time].Contains(message))
return false;
}
logger[timestamp].Add(message);
return true;
}
}
}