codekofi
← All problems

Problem 23

Hard

1 · Worked examples

0 / 3

Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. Only correct predictions count.

solution()
returns

2 · Which problem is it?

Locked until you have predicted 3 outputs correctly.

The accepted solution

1vector<double> solution(const vector<int>& arriving) {
2
3 priority_queue<int> low;
4 priority_queue<int, vector<int>, greater<int>> high;
5 vector<double> ans;
6
7 for (int x : arriving) {
8
9 low.push(x);
10 high.push(low.top());
11 low.pop();
12
13 if (high.size() > low.size()) {
14 low.push(high.top());
15 high.pop();
16 }
17
18 if (low.size() > high.size()) {
19 ans.push_back(low.top());
20 } else {
21 double middle = (low.top() + high.top()) / 2.0;
22 ans.push_back(middle);
23 }
24 }
25
26 return ans;
27}

Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.