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;
    }
}
posted @ 2020-09-07 11:16  EvanMeetTheWorld  阅读(13)  评论(0)    收藏  举报