codekofi
← All problems

Problem 95

Hard

1 · Worked examples

0 / 4

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 4 outputs correctly.

The accepted solution

1vector<int> solution(const vector<int>& nums, int k) {
2
3 int n = nums.size();
4 deque<int> window;
5 vector<int> ans;
6
7 for (int i = 0; i < n; i++) {
8
9 while (!window.empty() && window.front() <= i - k) {
10 window.pop_front();
11 }
12
13 while (!window.empty() && nums[window.back()] <= nums[i]) {
14 window.pop_back();
15 }
16
17 window.push_back(i);
18
19 if (i >= k - 1) {
20 ans.push_back(nums[window.front()]);
21 }
22 }
23
24 return ans;
25}

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.