共计 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;
}
};
正文完
请博主喝杯咖啡吧!