forked from piyush01123/Daily-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol.cpp
53 lines (41 loc) · 939 Bytes
/
sol.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <vector>
#include <time.h>
class HitCounter{
//hit counter class
public:
HitCounter(){}; //constructor
void record(time_t);
int total();
int range(int, int);
std::vector<time_t> hits;
};
void HitCounter::record(time_t t){
//store hit time in a vector; not efficient for limited memory
this->hits.push_back(t);
}
int HitCounter::total(){
return this->hits.size();
}
int HitCounter::range(int lower, int upper){
//no. of hits in a range
int i = 0;
while (this->hits[i]<lower) i++;
int j = i;
while (this->hits[j]<upper) j++;
return j-i+1;
}
void test(){
//builds and runs test cases
HitCounter hitctr;
time_t t = time(NULL);
for (int i=0; i<10; i++) hitctr.record(t+i);
std::cout << hitctr.total() << '\n';
std::cout << hitctr.range(t+2, t+6) << '\n';
}
int main(){
//run the test
// std::cout << "TIME = " << time(NULL) << '\n';
test();
return 0;
}