PDF版 ePub版

# Container With Most Water（最大水容器）

## 翻译

n 个垂直线段例如线段的两个端点在（i,ai）和（i,0）。

## 原文

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.

Note: You may not slant the container.

public int MaxAera(int[] height)
{
int area = 0;
for (int i = 0; i < height.Length; i++)
{
for (int j = i + 1; j < height.Length; j++)
{
if (height[i] < height[j])
area = Math.Max(area, countArea(height, i, j));
}
}
return area;
}

public int countArea(int[] height, int x, int y)
{
int h = height[x] > height[y] ? height[x] : height[y];
int info = h * (y - x);
return info;
}

maxArea = Max(maxArea,newArea);

int[] height = {10, 1, 2, 3, 4, 5, 6, 7, 11};

public class Solution
{
public int MaxArea(int[] height)
{
int left = 0, right = height.Length - 1;
int maxArea = 0;
while (left < right && left >= 0 && right <= height.Length - 1)
{
maxArea = Math.Max(maxArea, Math.Min(height[left], height[right]) * (right - left));
if (height[left] > height[right])
{
right--;
}
else
{
left++;
}
}
return maxArea;
}
}