To get the area between poles i created two pointers for the pair of poles that moves inward while calculating the areas between the poles also while limiting the height to be used for calculating the area to the shorter pole.
-
Time complexity: O(N)
-
Space complexity: O(1)
function maxArea(height: number[]): number {
let areas = []
let i = 0;
let j = height.length - 1
while (j > i) {
let width = j - i
if (height[i] < height[j]) {
areas.push(height[i] * width)
i++
} else {
areas.push(height[j] * width)
j--
}
}
return Math.max(...areas)
};