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 linei 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.
题的意思也很好理解。容量=min(ai, aj)*(j-i)。
1)O(N^2)的算法
class Solution {
public:
int maxArea(vector& height) {
// int i=0, j=0;
int maxV=0;
for(int i=0; i maxV) {
maxV = Vol;
}
}
}
return maxV;
}
};
class Solution{
public:
int maxArea(vector &height) {
int left=height[0], right=height[height.size()-1];
int i=0, j=height.size()-1;
int maxV=min(height[i], height[j])*(j-i);
while(i left || height[j] > right) {
int tempV=min(height[i], height[j])*(j-i);
if(tempV > maxV) {
maxV = tempV;
left=i;
right=j;
}
}
if(height[i] <= height[j])
++i;
else
--j;
}
cout<