codekofi
← All problems

Problem 134

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

1int solution(const vector<int>& heights) {
2
3 int n = heights.size();
4 vector<int> pending;
5 int ans = 0;
6
7 for (int i = 0; i <= n; i++) {
8
9 int h = 0;
10
11 if (i < n) {
12 h = heights[i];
13 }
14
15 while (!pending.empty() && heights[pending.back()] >= h) {
16
17 int top = heights[pending.back()];
18 pending.pop_back();
19
20 int left = -1;
21
22 if (!pending.empty()) {
23 left = pending.back();
24 }
25
26 int width = i - left - 1;
27 ans = max(ans, top * width);
28 }
29
30 pending.push_back(i);
31 }
32
33 return ans;
34}

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.