codekofi
← All problems

Problem 57

Easy

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<int> solution(int k, const vector<int>& seed,
2 const vector<int>& arriving) {
3
4 priority_queue<int, vector<int>, greater<int>> keep;
5
6 for (int x : seed) {
7
8 keep.push(x);
9
10 if ((int)keep.size() > k) {
11 keep.pop();
12 }
13 }
14
15 vector<int> ans;
16
17 for (int x : arriving) {
18
19 keep.push(x);
20
21 if ((int)keep.size() > k) {
22 keep.pop();
23 }
24
25 ans.push_back(keep.top());
26 }
27
28 return ans;
29}

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.