Largest water container
Mediumarraytwo pointersgreedy
heights[i] is the height of a wall at position i. Pick two walls that, with the ground, form a container holding (distance between them) × (height of the shorter one). Return the maximum water possible.
Example 1
Input: heights = [1,8,6,2,5,4,8,3,7] Output: 49
Walls 8 (position 1) and 7 (position 8): 7 × 7 = 49.
Example 2
Input: heights = [1,1] Output: 1
Constraints
- 2 ≤ heights.length ≤ 10⁵
- 0 ≤ heights[i] ≤ 10⁴
Submit checks your solution against 7 tests, including hidden ones.
/**
* @param {int[]} heights
* @return {int}
*/
function maxArea(heights) {
}