每日leetcode–Container With Most Water

3,175次阅读
没有评论

共计 549 个字符,预计需要花费 2 分钟才能阅读完成。

原题目:

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.

 


 盛水的多少由短板决定以及两个板之间的距离,木桶效应决定板的高度

class Solution {
public:
    int maxArea(vector& height) {
        int result=0;
        int lx=0;
        int rx=height.size()-1;
        if(rx<=0)
        {
            return 0;
        }
        
        while(lx<rx)
        {
            int tmp_area=(rx-lx)*min(height[lx],height[rx]);
     
            if(height[lx]<height[rx])             
            {                
                lx++;             
            }else             
            {                
                rx--;             
            }             
            if(tmp_area>result)
            {
                result=tmp_area;
            }
        }
        return result;
        
    }
};
正文完
请博主喝杯咖啡吧!
post-qrcode
 
admin
版权声明:本站原创文章,由 admin 2016-04-18发表,共计549字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码