codekofi
← All problems

Problem 144

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<int> solution(vector<vector<int>> spans,
2 const vector<int>& queries) {
3
4 int n = queries.size();
5 vector<pair<int, int>> asked(n);
6
7 for (int i = 0; i < n; i++) {
8 asked[i] = {queries[i], i};
9 }
10
11 sort(asked.begin(), asked.end());
12 sort(spans.begin(), spans.end());
13
14 using Item = pair<int, int>;
15 priority_queue<Item, vector<Item>, greater<Item>> live;
16
17 vector<int> ans(n, -1);
18 int at = 0;
19
20 for (const auto& [q, id] : asked) {
21
22 while (at < (int)spans.size() && spans[at][0] <= q) {
23
24 int width = spans[at][1] - spans[at][0] + 1;
25
26 live.push({width, spans[at][1]});
27 at++;
28 }
29
30 while (!live.empty() && live.top().second < q) {
31 live.pop();
32 }
33
34 if (!live.empty()) {
35 ans[id] = live.top().first;
36 }
37 }
38
39 return ans;
40}

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.