30 Day Challenge Day 11 | Leetcode 11. Container With Most Water
题解
双指针法
area = (j-i)*min(height[i], height[j]);
class Solution {
public:
int maxArea(vector<int>& height) {
int max_area = 0;
int l = 0, r = height.size()-1;
while(l < r) {
if(height[l] < height[r]) {
max_area = max(max_area, height[l]*(r-l));
l++;
} else {
max_area = max(max_area, height[r]*(r-l));
r--;
}
}
return max_area;
}
};