水桶装水问题

来源:互联网 发布:windows微信登录 编辑:程序博客网 时间:2024/04/30 03:54

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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.


想了很长时间,实际上处理的长方形的面积。假设横坐标长度为 x, 纵坐标长度为 y(取数组两端的最小值 ),求一个面积最大的长方形。

s = x*y

调整x和y值 达到 s最大。

class Solution {public:    int maxArea(vector<int>& height) {        int l =0;        int h = height.size();        int vol =0;        h--;              int max=0;        while(l<h)        {            int min = height[l]>height[h]?height[h]:height[l];            vol = min*(h-l);            if(vol>max)                max = vol;            if(height[l]<height[h])                l++;            else               h--;        }        return max;    }};


0 0
原创粉丝点击