Container With Most Water
classic water container problems.
using left and right pointer. find the one that constraint the height of this container, and move to find a potential larger one.
class Solution {
public int maxArea(int[] height) {
int res = 0;
int l = 0;
int r = height.length - 1;
while (l < r) {
res = Math.max(res, Math.min(height[r], height[l]) * (r - l));
if (height[r] > height[l]) {
l++;
} else {
r--;
}
}
return res;
}
}

浙公网安备 33010602011771号