You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
Notice that you may not slant the container.
Example 1:
Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example 2:
Input: height = [1,1] Output: 1
Constraints:
n == height.length2 <= n <= 1050 <= height[i] <= 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a series of vertical walls of different heights. The brute force strategy is to figure out the amount of water every single possible pair of walls could hold between them, and then simply pick the pair that holds the most.
Here's how the algorithm would work step-by-step:
def maxArea(height):
max_water_volume = 0
# Iterate through each wall to select it as the first potential side of the container.
for left_wall_index in range(len(height)):
# Now, pair the selected left wall with every other wall to its right.
for right_wall_index in range(left_wall_index + 1, len(height)):
distance_between_walls = right_wall_index - left_wall_index
# The container's capacity is limited by the shorter of the two walls.
container_height = min(height[left_wall_index], height[right_wall_index])
current_water_volume = container_height * distance_between_walls
# We must keep track of the largest volume found across all possible pairs.
if current_water_volume > max_water_volume:
max_water_volume = current_water_volume
return max_water_volumeThe best strategy starts with the widest possible container and cleverly narrows it down. By always moving the shorter of the two sides inward, we can skip checking many combinations that are guaranteed to hold less water, quickly finding the largest possible area.
Here's how the algorithm would work step-by-step:
def max_area(height_of_lines):
max_water_area = 0
left_pointer = 0
right_pointer = len(height_of_lines) - 1
while left_pointer < right_pointer:
left_height = height_of_lines[left_pointer]
right_height = height_of_lines[right_pointer]
container_width = right_pointer - left_pointer
# The amount of water is always limited by the shorter of the two vertical lines.
container_height = min(left_height, right_height)
current_water_area = container_width * container_height
max_water_area = max(max_water_area, current_water_area)
# Moving the shorter line's pointer is the only way to potentially find a larger area.
if left_height < right_height:
left_pointer += 1
else:
# If heights are equal, moving either pointer works, as this width has been fully explored.
right_pointer -= 1
return max_water_area| Case | How to Handle |
|---|---|
| Input array has fewer than 2 elements (0 or 1) | No container can be formed, so the maximum water is 0. |
| Input array has exactly 2 elements | The only possible container is formed by these two lines, and its area is the solution. |
| All heights in the array are identical (e.g., [5, 5, 5, 5]) | The optimal solution correctly identifies the widest pair (the first and last lines) as the one holding the most water. |
| All heights are zero (e.g., [0, 0, 0, 0]) | The height of any container formed will be 0, so the maximum water capacity is correctly calculated as 0. |
| Heights are in a strictly increasing or decreasing order (e.g., [1, 2, 3, 4] or [4, 3, 2, 1]) | The two-pointer approach efficiently narrows the search space by correctly moving the pointer at the shorter line inward. |
| Very large number of lines (e.g., 10^5 elements) with large heights | The area calculation (width * height) could result in a large number, which must fit within standard integer types like a 64-bit integer to avoid overflow. |
| Optimal container is defined by two very tall lines close together, surrounded by short lines | The algorithm correctly evaluates all potential containers, including narrow but tall ones, to find the true maximum. |
| Optimal container is defined by two short lines very far apart | The two-pointer approach starts with the widest possible container and guarantees this configuration is considered first. |