11 盛最多水的容器
题目描述
题目给出的接口:
class Solution {
public:
int maxArea(vector<int>& height) {
}
};
题目分析
找出相对最短边,算出盛水体积,得到最大盛水体积。
class Solution {
public:
int maxArea(vector<int>& height) {
int i=0;
int j=height.size()-1;
int res=0;
while(i<j)
{
int temp1 = height[i]<height[j]?height[i] : height[j]; //找出相对最短边
int temp2 = temp1 * (j-i); //计算相对盛水面积
res = res > temp2 ? res : temp2; //找出最大面积
if(height[i]<height[j]) i ++;
else j --;
}
return res;
}
};