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.length
2 <= n <= 105
0 <= height[i] <= 104
When 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:
The problem asks us to find the largest container that can be formed by selecting two vertical lines from a given set of lines. The brute force method involves checking every single possible pair of lines to see which combination forms the largest container. We calculate the area for each pair and keep track of the biggest one we find.
Here's how the algorithm would work step-by-step:
def max_area_brute_force(heights):
max_area = 0
# Iterate through each line as the potential left boundary.
for left_index in range(len(heights)):
# Compare the left boundary with all other lines to find the max area
for right_index in range(left_index + 1, len(heights)):
# Determine the height of the container formed.
current_height = min(heights[left_index], heights[right_index])
#Calculate area using width and bounded height.
current_width = right_index - left_index
area = current_height * current_width
#Update maximum if larger area is found.
max_area = max(max_area, area)
return max_area
The goal is to find two vertical lines that, along with the bottom axis, will hold the most water. Instead of checking every possible pair of lines, we cleverly narrow down our choices by moving inwards from the widest possible container.
Here's how the algorithm would work step-by-step:
def max_area(heights):
max_water = 0
left_index = 0
right_index = len(heights) - 1
while left_index < right_index:
# Calculate current container's area
current_width = right_index - left_index
current_height = min(heights[left_index], heights[right_index])
current_water = current_width * current_height
max_water = max(max_water, current_water)
# Move pointer of the shorter line inward
if heights[left_index] < heights[right_index]:
left_index += 1
# Shorter line limits water, move to find a taller line
else:
right_index -= 1
# Shorter line limits water, move to find a taller line
return max_water
Case | How to Handle |
---|---|
Empty or null height array | Return 0 immediately since no container can be formed. |
Height array with only one element | Return 0 since a container needs at least two lines. |
Height array with only two elements | Calculate the area formed by these two lines and return it. |
Height array with all elements equal to 0 | The algorithm should return 0 as the container can hold no water. |
Height array with very large values that could cause integer overflow when calculating area | Use long to store the area to prevent potential overflow. |
Height array where all elements are in descending order | The algorithm correctly explores container areas even when heights are non-increasing. |
Height array where all elements are in ascending order | The algorithm correctly explores container areas even when heights are non-decreasing. |
Height array contains duplicate heights | The algorithm handles duplicate heights correctly as it considers all possible pairs. |