class Solution:
def maxArea(self, height: List[int]) -> int:
maxArea = float('-inf')
start = 0
end = len(height)
while start < end:
tmp = (end - (start + 1)) * min(height[start], height[end-1])
maxArea = max(tmp, maxArea)
if height[start] < height[end-1]:
start += 1
else:
end -= 1
return maxArea
Java实现
public class ContainerWithMostWater_11 {
public int maxArea(int[] height) {
int left = 0;
int right = height.length-1;
int maxArea = 0;
while(left < right){
int tmpArea = (right - left) * Math.min(height[left], height[right]);
maxArea = Math.max(tmpArea, maxArea);
if (height[left] < height[right]){
left ++;
}else{
right --;
}
}
return maxArea;
}
@Test
public void TestContainerWithMostWater(){
int[] height = {1,8,6,2,5,4,8,3,7};
System.out.println(maxArea(height));
}
}