-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
83 lines (74 loc) · 2.79 KB
/
main.cpp
File metadata and controls
83 lines (74 loc) · 2.79 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Source: https://leetcode.com/problems/design-hit-counter
// Title: Design Hit Counter
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Design a hit counter which counts the number of hits received in the past `5` minutes (i.e., the past `300` seconds).
//
// Your system should accept a `timestamp` parameter (**in seconds** granularity), and you may assume that calls are being made to the system in chronological order (i.e., `timestamp` is monotonically increasing). Several hits may arrive roughly at the same time.
//
// Implement the `HitCounter` class:
//
// - `HitCounter()` Initializes the object of the hit counter system.
// - `void hit(int timestamp)` Records a hit that happened at `timestamp` (**in seconds**). Several hits may happen at the same `timestamp`.
// - `int getHits(int timestamp)` Returns the number of hits in the past 5 minutes from `timestamp` (i.e., the past `300` seconds).
//
// **Example 1:**
//
// ```
// Input
//
// ["HitCounter", "hit", "hit", "hit", "getHits", "hit", "getHits", "getHits"]
// [[], [1], [2], [3], [4], [300], [300], [301]]
// Output
//
// [null, null, null, null, 3, null, 4, 3]
//
// Explanation
//
// HitCounter hitCounter = new HitCounter();
// hitCounter.hit(1); // hit at timestamp 1.
// hitCounter.hit(2); // hit at timestamp 2.
// hitCounter.hit(3); // hit at timestamp 3.
// hitCounter.getHits(4); // get hits at timestamp 4, return 3.
// hitCounter.hit(300); // hit at timestamp 300.
// hitCounter.getHits(300); // get hits at timestamp 300, return 4.
// hitCounter.getHits(301); // get hits at timestamp 301, return 3.
// ```
//
// **Constraints:**
//
// - `1 <= timestamp <= 2 * 10^9`
// - All the calls are being made to the system in chronological order (i.e., `timestamp` is monotonically increasing).
// - At most `300` calls will be made to `hit` and `getHits`.
//
// **Follow up:** What if the number of hits per second could be huge? Does your design scale?
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <queue>
using namespace std;
// Queue
class HitCounter {
constexpr static int fiveMin = 300;
using Item = pair<int, int>; // (time, hits)
queue<Item> data;
int total;
public:
HitCounter() : total(0) {}
void hit(int timestamp) {
if (!data.empty() && timestamp == data.back().first) {
++data.back().second;
} else {
data.push({timestamp, 1});
}
++total;
}
int getHits(int timestamp) {
// Remove old item
while (!data.empty() && data.front().first <= timestamp - fiveMin) {
total -= data.front().second;
data.pop();
}
return total;
}
};