func maxArea(height []int) int {
}func maxArea(height []int) int {
// Area = width × height. The widest possible container is the outermost
// pair, so start there and only move inward when it might pay off.
}func maxArea(height []int) int {
// Area = width × height. The widest possible container is the outermost
// pair, so start there and only move inward when it might pay off.
left, right := 0, len(height)-1
best := 0
}func maxArea(height []int) int {
// Area = width × height. The widest possible container is the outermost
// pair, so start there and only move inward when it might pay off.
left, right := 0, len(height)-1
best := 0
for left < right {
}
}func maxArea(height []int) int {
// Area = width × height. The widest possible container is the outermost
// pair, so start there and only move inward when it might pay off.
left, right := 0, len(height)-1
best := 0
for left < right {
// Water spills over the shorter wall, so the usable height is the
// minimum of the two ends. Width is the distance between them.
}
}func maxArea(height []int) int {
// Area = width × height. The widest possible container is the outermost
// pair, so start there and only move inward when it might pay off.
left, right := 0, len(height)-1
best := 0
for left < right {
// Water spills over the shorter wall, so the usable height is the
// minimum of the two ends. Width is the distance between them.
h := min(height[left], height[right])
best = max(best, h*(right-left))
}
}func maxArea(height []int) int {
// Area = width × height. The widest possible container is the outermost
// pair, so start there and only move inward when it might pay off.
left, right := 0, len(height)-1
best := 0
for left < right {
// Water spills over the shorter wall, so the usable height is the
// minimum of the two ends. Width is the distance between them.
h := min(height[left], height[right])
best = max(best, h*(right-left))
// Moving inward always loses width, so it's only worth it if we can gain
// height. The shorter wall caps this pair; keeping it can never beat what
// we just recorded — so discard it and hope the next wall is taller.
}
}func maxArea(height []int) int {
// Area = width × height. The widest possible container is the outermost
// pair, so start there and only move inward when it might pay off.
left, right := 0, len(height)-1
best := 0
for left < right {
// Water spills over the shorter wall, so the usable height is the
// minimum of the two ends. Width is the distance between them.
h := min(height[left], height[right])
best = max(best, h*(right-left))
// Moving inward always loses width, so it's only worth it if we can gain
// height. The shorter wall caps this pair; keeping it can never beat what
// we just recorded — so discard it and hope the next wall is taller.
if height[left] < height[right] {
left++
} else {
right--
}
}
}func maxArea(height []int) int {
// Area = width × height. The widest possible container is the outermost
// pair, so start there and only move inward when it might pay off.
left, right := 0, len(height)-1
best := 0
for left < right {
// Water spills over the shorter wall, so the usable height is the
// minimum of the two ends. Width is the distance between them.
h := min(height[left], height[right])
best = max(best, h*(right-left))
// Moving inward always loses width, so it's only worth it if we can gain
// height. The shorter wall caps this pair; keeping it can never beat what
// we just recorded — so discard it and hope the next wall is taller.
if height[left] < height[right] {
left++
} else {
right--
}
}
return best // O(n) time, O(1) space
}Tap the left half to go back, the right half to go forward.
Arrow keys or space to navigate · Esc to exit.