-
Notifications
You must be signed in to change notification settings - Fork 3
/
1825.cpp
74 lines (72 loc) · 1.7 KB
/
1825.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class MKAverage {
public:
long long sum;
int M;
int K;
int size;
queue<int> Q;
multiset<int> left, middle, right;
MKAverage(int m, int k) {
sum = 0;
M = m;
K = k;
size = m - k * 2;
}
void remove(int num) {
if (num <= *(left.rbegin())) {
left.erase(left.find(num));
}
else if (num <= *(middle.rbegin())) {
middle.erase(middle.find(num));
sum -= num;
}
else {
right.erase(right.find(num));
}
if (left.size() < K) {
auto it = middle.begin();
left.insert(*it);
sum -= *it;
middle.erase(it);
}
if (middle.size() < size) {
auto it = right.begin();
middle.insert(*it);
sum += *it;
right.erase(it);
}
}
void add(int num) {
left.insert(num);
if (left.size() > K) {
auto it = prev(end(left));
middle.insert(*it);
sum += *it;
left.erase(it);
}
if (middle.size() > size) {
auto it = prev(end(middle));
right.insert(*it);
sum -= *it;
middle.erase(it);
}
}
void addElement(int num) {
Q.push(num);
if (Q.size() > M) {
remove(Q.front());
Q.pop();
}
add(num);
}
int calculateMKAverage() {
if (Q.size() < M) return -1;
return sum / size;
}
};
/**
* Your MKAverage object will be instantiated and called as such:
* MKAverage* obj = new MKAverage(m, k);
* obj->addElement(num);
* int param_2 = obj->calculateMKAverage();
*/