codekofi
← All problems

Problem 9

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

1int solution(const vector<int>& nums) {
2
3 if (nums.empty()) {
4 return 0;
5 }
6
7 int left = 0;
8 int right = nums.size() - 1;
9 int ans = 0;
10
11 while (left < right) {
12
13 int height = min(nums[left], nums[right]);
14 int width = right - left;
15 int area = height * width;
16
17 if (area > ans) {
18 ans = area;
19 }
20
21 if (nums[left] < nums[right]) {
22 left++;
23 } else {
24 right--;
25 }
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.