思路:1.距离越远面积越大 2.面积由较短木条决定
算法:双指针分别指向数组首尾,木条较短的一段向内收缩(不断尝试将较短边增长),直至重合。
时间复杂度:O(N)
C++代码:
class Solution {
public:
int maxArea(vector<int>& height) {
int max = 0;
vector<int>::iterator it_front = height.begin(), it_back = height.end() - 1;
while (it_front != it_back)
{
int area = (it_back - it_front)*min(*it_front, *it_back);
if (area > max)
max = area;
if (*it_front <= *it_back)
it_front++;
else
it_back--;
}
return max;
}
};