-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1825_finding-mk-average.js
197 lines (194 loc) · 4.07 KB
/
1825_finding-mk-average.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Title: Finding MK Average
* Description: You are given two integers, m and k, and a stream of integers. You are tasked to implement a data structure that calculates the MKAverage for the stream.
* Author: Hasibul Islam
* Date: 28/04/2023
*/
function Bisect() {
return { insort_right, insort_left, bisect_left, bisect_right };
function insort_right(a, x, lo = 0, hi = null) {
lo = bisect_right(a, x, lo, hi);
a.splice(lo, 0, x);
}
function bisect_right(a, x, lo = 0, hi = null) {
// > upper_bound
if (lo < 0) throw new Error("lo must be non-negative");
if (hi == null) hi = a.length;
while (lo < hi) {
let mid = parseInt((lo + hi) / 2);
a[mid] > x ? (hi = mid) : (lo = mid + 1);
}
return lo;
}
function insort_left(a, x, lo = 0, hi = null) {
lo = bisect_left(a, x, lo, hi);
a.splice(lo, 0, x);
}
function bisect_left(a, x, lo = 0, hi = null) {
// >= lower_bound
if (lo < 0) throw new Error("lo must be non-negative");
if (hi == null) hi = a.length;
while (lo < hi) {
let mid = parseInt((lo + hi) / 2);
a[mid] < x ? (lo = mid + 1) : (hi = mid);
}
return lo;
}
}
function MultiSet(elements) {
let a = [],
m = new Map(),
bi = new Bisect();
initialize();
return {
insert,
first,
last,
get,
poll,
pollLast,
lower_bound,
upper_bound,
findKth,
remove,
removeAll,
contains,
size,
clear,
show,
};
function initialize() {
if (elements) {
for (const x of elements) {
bi.insort_right(a, x);
m.set(x, m.get(x) + 1 || 1);
}
}
}
function insert(x) {
bi.insort_right(a, x);
m.set(x, m.get(x) + 1 || 1);
}
function first() {
return a[0];
}
function last() {
return a[a.length - 1];
}
function get(i) {
return a[i];
}
function poll() {
let res = a[0];
a.splice(0, 1);
removeOneOrManyMap(m, res);
return res;
}
function pollLast() {
let res = a.pop();
removeOneOrManyMap(m, res);
return res;
}
function lower_bound(x) {
return bi.bisect_left(a, x);
}
function upper_bound(x) {
return bi.bisect_right(a, x);
}
function findKth(k) {
return a[k - 1];
}
function remove(x) {
let idx = lower_bound(x);
if (a[idx] == x) a.splice(idx, 1);
removeOneOrManyMap(m, x);
}
function removeAll(x) {
if (contains(x)) {
let idx = search(x),
occ = m.get(x);
while (occ--) a.splice(idx, 1);
m.delete(x);
}
}
function removeOneOrManyMap(m, x, cnt = 1) {
let occ = m.get(x);
occ > cnt ? m.set(x, occ - cnt) : m.delete(x);
}
function contains(x) {
return m.has(x);
}
function size() {
return a.length;
}
function clear() {
a = [];
m.clear();
}
function show() {
return a;
}
}
////////////////////////////////////////////////////////////////////////
function MKAverage(m, k) {
let L = new MultiSet(),
R = new MultiSet(),
M = new MultiSet(),
a = [],
sum = 0,
pos = 0,
sz = m - 2 * k;
return { addElement, calculateMKAverage };
function addElement(x) {
add(x);
if (pos >= m) remove(a[pos % m]);
a[pos++ % m] = x;
}
function calculateMKAverage() {
if (pos < m) return -1;
return (sum / sz) >> 0;
}
function add(x) {
L.insert(x);
if (L.size() > k) {
let v = L.last();
M.insert(v);
sum += v;
L.remove(v);
}
if (M.size() > sz) {
let v = M.last();
sum -= v;
R.insert(v);
M.remove(v);
}
}
function remove(x) {
if (x <= L.last()) {
L.remove(x);
} else if (x <= M.last()) {
sum -= x;
M.remove(x);
} else {
R.remove(x);
}
if (L.size() < k) {
let v = M.first();
L.insert(v);
sum -= v;
M.poll();
}
if (M.size() < sz) {
let v = R.first();
M.insert(v);
sum += v;
R.poll();
}
}
}
/**
* Your MKAverage object will be instantiated and called as such:
* var obj = new MKAverage(m, k)
* obj.addElement(num)
* var param_2 = obj.calculateMKAverage()
*/