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 row of vertical walls of different heights and you want to pick any two walls to form a container that holds the most water. The brute force approach involves trying every single possible pair of walls, measuring how much water each pair can hold, and keeping track of the largest amount found.
Here's how the algorithm would work step-by-step:
def max_area(heights): maximum_water = 0 total_walls = len(heights) # Evaluate every possible pair of walls to find the maximum possible container volume for left_index in range(total_walls): for right_index in range(left_index + 1, total_walls): distance_between = right_index - left_index shorter_height = min(heights[left_index], heights[right_index]) current_water = distance_between * shorter_height # Track the peak water capacity found across all evaluated wall pairings if current_water > maximum_water: maximum_water = current_water return maximum_waterInstead of checking every single combination of walls to find the biggest area, the optimal approach uses a clever narrowing strategy from the outer edges inward. By starting with the widest possible container and smartly moving our boundaries based on height, we can quickly find the maximum possible water capacity.
Here's how the algorithm would work step-by-step:
def max_area(height_list):
left_marker = 0
right_marker = len(height_list) - 1
max_water_seen = 0
while left_marker < right_marker:
# Width decreases, so we must greedily move the shorter wall to seek a taller replacement.
width_between = right_marker - left_marker
height_left = height_list[left_marker]
height_right = height_list[right_marker]
limiting_height = min(height_left, height_right)
current_capacity = width_between * limiting_height
# Track the absolute maximum capacity found across all pointer positions.
if current_capacity > max_water_seen:
max_water_seen = current_capacity
# The shorter wall limits capacity; keeping it can never yield a larger area with new widths.
if height_left < height_right:
left_marker += 1
else:
right_marker -= 1
return max_water_seen| Case | How to Handle |
|---|---|
| Array with fewer than two elements | Return zero immediately since a container requires at least two vertical lines. |
| Array with exactly two elements | Calculate the area for the single possible pair and return that value as the maximum. |
| Heights containing zeros | The two-pointer approach naturally moves past zeros because the minimum height formula results in zero area, driving the pointer inward. |
| Array with all identical height values | The initial outer pointers calculate the maximum possible width container, and subsequent pointer movements safely evaluate smaller widths. |
| Highly skewed distributions such as tall walls on the outside and short walls in the middle | The greedy two-pointer strategy ensures we always discard the shorter line, guaranteeing we do not miss the optimal container. |
| Extremely large inputs with maximum constraint sizes | An O(n) linear scan using two pointers processes millions of elements efficiently within time limits without exceeding memory or recursion limits. |
| Maximum possible integer values for heights and maximum array length | Use 64-bit integers for the area calculation to prevent potential integer overflow when multiplying maximum width by maximum height. |