Problem · Array

Container With Most Water

Learn this problem
MediumeBay logoeBayFULLTIMEONSITE INTERVIEW

Problem statement

You are given an integer array heights. At index i, a vertical line extends from (i, 0) to (i, heights[i]).

Choose two distinct lines. Together with the horizontal axis, they form a container whose area is (right - left) * min(heights[left], heights[right]). Return the maximum area obtainable from any pair of lines.

Function

maxArea(heights: int[]) → int

Examples

Example 1

heights = [1,8,6,2,5,4,8,3,7]return = 49

The lines at indices 1 and 8 have limiting height 7 and width 7, producing area 49.

Example 2

heights = [1,1]return = 1

The only two lines have width 1 and height 1.

Example 3

heights = [4,3,2,1,4]return = 16

The first and last lines have height 4 and width 4, producing area 16.

Constraints

  • 2 <= heights.length <= 100000.
  • 0 <= heights[i] <= 10000.

More eBay problems

drafts saved locally
public int maxArea(int[] heights) {
    // Write your code here.
}
heights[1,8,6,2,5,4,8,3,7]
expected49
checking account