-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy path0362-DesignHitCounter.cs
48 lines (40 loc) · 1.39 KB
/
0362-DesignHitCounter.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
42
43
44
45
46
47
48
//-----------------------------------------------------------------------------
// Runtime: 104ms
// Memory Usage: 24.4 MB
// Link: https://leetcode.com/submissions/detail/369498426/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0362_DesignHitCounter
{
private readonly Queue<int> queue;
/** Initialize your data structure here. */
public _0362_DesignHitCounter()
{
queue = new Queue<int>();
}
/** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
public void Hit(int timestamp)
{
while (queue.Count > 0 && queue.Peek() <= timestamp - 300)
queue.Dequeue();
queue.Enqueue(timestamp);
}
/** Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity). */
public int GetHits(int timestamp)
{
while (queue.Count > 0 && queue.Peek() <= timestamp - 300)
queue.Dequeue();
return queue.Count;
}
}
/**
* Your HitCounter object will be instantiated and called as such:
* HitCounter obj = new HitCounter();
* obj.Hit(timestamp);
* int param_2 = obj.GetHits(timestamp);
*/
}