Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.輸入一個數組,數組的每個元素都表明了一條垂直的線,其中每個元素的位置表明橫座標,元素的值表明縱座標。咱們須要找出這些線所圍成的「容器」,能裝最多水的水量。數組
public int maxArea(int[] height) { int maxArea = 0; int start = 0; int end = height.length-1; while(start < end){ maxArea = Math.max(maxArea, Math.min(height[start], height[end])*(end-start)); if(height[start] < height[end]) start ++; else end--; } return maxArea; }