javascript – Container with more water

Homework
is taken from leetcode

Given n non-negative integers a1, a2, …, an, where each represents
a point on the coordinate (i, ai). n vertical lines are drawn in such a way that
the two end points of line i are in (i, ai) and (i, 0). Find two lines,
that together with the x axis forms a container, so that the container
It contains the largest amount of water.

Note: you can not tilt the container and n is at least 2.

My first solution:

``````/ **
* @param {number[]height
* @return {number}
* /
var maxArea = height => {
leave max = 0;
height.for each ((hL, i) => {
height.slice (i + 1) .forEach ((hR, j) => {
max = Math.max (max, Math.min (hL, hR) * (1 + j));
});
});
return max;
};
``````

My second solution:

``````/ **
* @param {number[]height
* @return {number}
* /
var maxArea2 = height => {
Let l = max = 0;
be r = height.length - 1;
while (l < r) {
const area = Math.min(height[l], height[r]) * (r - l);
if (height[l] > height[r]) {
r--;
} else {
l ++;
}
max = Math.max (max, area);
}
return max;
};
``````

Is it possible to solve it mathematically and do it faster?