codekofi
← All problems

Problem 53

Medium

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>& temps) {
2
3 int n = temps.size();
4 vector<int> ans(n, 0);
5 stack<int> pending;
6
7 for (int i = 0; i < n; i++) {
8
9 while (!pending.empty() && temps[pending.top()] < temps[i]) {
10
11 int earlier = pending.top();
12 pending.pop();
13
14 ans[earlier] = i - earlier;
15 }
16
17 pending.push(i);
18 }
19
20 return ans;
21}

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.