11 Container With Most Water
链接:https://leetcode.com/problems/container-with-most-water/
问题描述:
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.
Hide Tags Array Two Pointers
Hide Similar Problems (H) Trapping Rain Water
给出数组,数组大小代表墙的高度,找到两堵墙,使他们之间可以容纳最多的水。
class Solution {
public:
int maxArea(vector<int> &height)
{
int result=0;
int p1=0,p2=height.size();
while(p1<p2)
{
if(min(height[p1],height[p2])*(p2-p1)>result)
result=min(height[p1],height[p2])*(p2-p1);
if(height[p1]<height[p2])
p1++;
else
p2--;
}
return result;
}
};