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;
}
posted @ 2012-09-05 11:00  紫红的泪  阅读(9911)  评论(0编辑  收藏  举报