Container With Most Water
题意是有个高度数组,就相当于隔板的高度,求数组中任意两隔板间盛水的最大量。隔板间的距离与较低隔板的高度乘积即为盛水的容量。
int maxArea(vector<int> &height)
{
int capability = 0;
size_t left = 0, right = height.size() - 1;
while (left < right)
{
const int water =
min(height[left], height[right]) * (right - left);
if (water > capability) capability = water;
if (height[left] < height[right])
{
++left;
}
else
{
--right;
}
}
return capability;
}