思路:两边往中间扫,哪边高度小,哪边就是蓄水的瓶颈,需要往里移动。
/*
* @lc app=leetcode id=11 lang=cpp
*
* [11] Container With Most Water
*/
// @lc code=start
class Solution {
public:
int maxArea(vector<int>& height) {
int R = height.size()-1;
int L = 0;
int ans = 0;
while(L<R){
ans = max(ans,(R-L)*min(height[L],height[R]));
if(height[L] > height[R]){
R--;
}else{
L++;
}
}
return ans;
}
};
// @lc code=end