Given *n* non-negative integers *a _{1}*,

*a*, …,

_{2}*a*, where each represents a point at coordinate (

_{n}*i*,

*a*).

_{i}*n*vertical lines are drawn such that the two endpoints of line

*i*is at (

*i*,

*a*) and (

_{i}*i*, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container.

public class Solution { public int maxArea(int[] height) { int left = 0, right = height.length-1; int max = 0; //max area while (left < right) { int h = Math.min(height[left], height[right]); int w = right - left; max = Math.max(max, h*w); //important: if (height[left] <= height[right]) left++; else right--; } return max; } }

Advertisements