盛最多水的容器(双指针法)
This commit is contained in:
parent
16b0c2ae64
commit
8dc2f5e9a5
17
maxArea.cpp
17
maxArea.cpp
@ -9,18 +9,21 @@ public:
|
||||
int maxArea(vector<int> &height)
|
||||
{
|
||||
int maxArea = 0;
|
||||
for(int i = 0; i < height.size(); i++)
|
||||
{
|
||||
for(int j = i + 1; j < height.size(); j++)
|
||||
{
|
||||
int area = min(height[i], height[j]) * (j - i);
|
||||
maxArea = max(maxArea, area);
|
||||
int left = 0;
|
||||
int right = height.size() - 1;
|
||||
while (left < right){
|
||||
maxArea = max(maxArea,(right - left)*min(height[right] , height[left]));
|
||||
if( height[left] < height[right]){
|
||||
left++;
|
||||
}
|
||||
else{
|
||||
right--;
|
||||
}
|
||||
}
|
||||
|
||||
return maxArea;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
|
||||
|
Loading…
x
Reference in New Issue
Block a user