双指针
class Solution {
public:
int maxArea(vector<int>& height) {
int out=0,n=height.size();
int slow=0,fast=n-1;
while(slow<fast){
out=max(out,(fast-slow)*min(height[slow],height[fast]));
if(height[slow]<height[fast])slow++;
else fast--;
}
return out;
}
};
暴力解超时
class Solution {
public:
int maxArea(vector<int>& height) {
int out=0,n=height.size();
for(int i=0;i<n-1;i++){
int temp=0;
for(int j=i+1;j<n;j++){
temp=max(temp,(j-i)*min(height[i],height[j]));
}
out=max(out,temp);
}
return out;
}
};