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.
Note: You may not slant the container and n is at least 2.
大致说下题意,就是在坐标面上有n个height,然后每个height与x轴连线,最后取任意两条线,使得两条线与x轴所构成的容器能装最多的水。水的面积不是容器的面积,而是最大矩形的面积。
class Solution {
public:
int maxArea(vector<int>& height) {
int len = height.size();
int max = 0, sign;
for(int i = 0; i < len - 1; i++){
if(height[i] < height[i -1] && i != 0) continue;
for(int j = len - 1; j > i; j--){
if(height[j] < height[i]){
if(max < height[j] * (j -i))
max = height[j] * (j -i);
}
else {
if(max < height[i] * (j -i))
max = height[i] * (j -i);
break;
}
}
}
return max;
}
};