Container With Most Water
Medium
Array
Two Pointers
Greedy
You are given an integer array `height` of length `n`. There are `n` vertical lines drawn such that the two endpoints of the `i`th line are `(i, 0)` and `(i, height[i])`. Find two lines that, together with the x-axis, form a container that holds the most water. Return *the maximum amount of water a container can store*.
Example 1
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The max area of water the container can contain is 49.
Example 2
Input: height = [1,1]
Output: 1

Constraints

  • n == height.length
  • 2 <= n <= 10⁵
  • 0 <= height[i] <= 10⁴
Time Complexity
O(n)
Space Complexity
O(1)
14
Case 1
Input: [1,8,6,2,5,4,8,3,7]
Expected: 49
Case 2
Input: [1,1]
Expected: 1
Case 3
Input: [4,3,2,1,4]
Expected: 16