- tags: Two Pointers,Tricky,LeetCode101
The key ideas are:
- We start from two edges and move to the middle with two pointers.
- Move the pointer to the middle which side is smaller.
class Solution {
public:
int maxArea(vector<int>& height) {
int i = 0, j = height.size() - 1;
int water = 0;
while (i < j) {
water = max(water, (j - i) * min(height[i], height[j]));
if (height[i] > height[j]) {
--j;
} else {
++i;
}
}
return water;
}
};