Find two lines that together with the x-axis form a container, such that the container contains the most water.
Code Playground
JavaScript (V8)
Loading...
Console Output
// Output will appear here...
// For React: ReactDOM.createRoot(rootElement).render(<App />);
// For React: ReactDOM.createRoot(rootElement).render(<App />);
Recommended Answer
js
function maxArea(height) {
let l = 0, r = height.length - 1;
let max = 0;
while (l < r) {
const w = r - l;
const h = Math.min(height[l], height[r]);
max = Math.max(max, w * h);
if (height[l] < height[r]) l++;
else r--;
}
return max;
}