Table of contents
Problem - Leetcode
You are given an integer array height
of length n
. There are n
vertical lines drawn such that the two endpoints of the i<sup>th</sup>
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 <= 10<sup>5</sup>
0 <= height[i] <= 10<sup>4</sup>
Answer-1 in Golang
func maxArea(height []int) int {
left, right := 0, (len(height) - 1)
result := 0
for left < right {
area := min(height[left], height[right]) * (right - left)
if result < area {
result = area
}
if height[left] > height[right] {
right--
} else {
left++
}
}
return result
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
The code implements a solution to the "Container With Most Water" problem using a two-pointer approach. This problem involves finding the maximum area that can be formed by choosing two lines on a coordinate plane and a vertical line drawn between them. The goal is to determine the maximum area that can be enclosed by these lines and the x-axis.
Here's a detailed breakdown of the code:
maxArea
function: This is the main function that calculates the maximum area for the given array of heights.left
andright
pointers: These pointers are initialized to the start and end of theheight
array, respectively. They will be used to represent the two lines forming the "container" whose area we want to maximize.result
variable: This variable is used to store the maximum area encountered during the process. It's initially set to 0.for
loop: The loop runs while theleft
pointer is less than theright
pointer. This loop iterates through different combinations of lines to calculate the maximum area.area
calculation: The area is calculated using themin
function to find the minimum height between the lines represented byheight[left]
andheight[right]
. This height is then multiplied by the width, which is the difference betweenright
andleft
. The formula used is:area = min(height[left], height[right]) * (right - left)
.Comparing and updating
result
: The calculatedarea
is compared with the current value ofresult
. If the calculated area is greater, it becomes the newresult
value, storing the maximum area encountered so far.Pointer movement: Depending on the heights of the lines at positions
left
andright
, either theleft
pointer is moved to the right or theright
pointer is moved to the left. This movement is done to explore different combinations of lines and find the optimal arrangement that maximizes the area.min
function: This function takes two integers as arguments and returns the minimum of the two.Returning the result: Once the
left
pointer is no longer less than theright
pointer, the loop exits, and the function returns theresult
, which represents the maximum area that can be formed by choosing two lines from theheight
array.
In essence, the code uses a two-pointer approach to iteratively consider different combinations of lines and calculate the areas formed by them, eventually finding the maximum area possible. The algorithm optimizes the search by moving the pointers strategically based on the heights of the lines being considered.