Two pointers · Medium
A geometric planning tool models vertical supports one unit apart. Choosing two supports defines a container whose usable height is limited by the shorter support. Intermediate supports do not reduce its area; this is not a trapped-rainwater calculation.
Return the largest area defined by any two distinct supports in the supplied height sequence.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ heights: integer[] }
Fields
- heights contains non-negative integer heights. Support i is at horizontal position i.
Output
Return value
- For indexes i < j, area is min(heights[i],heights[j]) * (j - i). Return the largest area.
- Return 0 when fewer than two supports exist. Return area, not indexes or total water between every pair.
Constraints
- 0 <= heights.length <= 100000; 0 <= heights[i] <= 1000000000.
- Every area fits exactly in a safe JSON integer.
Examples
Example 1
{
"heights": [
1,
8,
6,
2,
5,
4,
8,
3,
7
]
}Returns
49Example 2
{
"heights": [
5,
5
]
}Returns
5Required complexity
O(n) time and O(1) auxiliary space