본문 바로가기
알고리즘/LeetCode

[Leetcode][Javascript][Medium] 11. Container With Most Water

by Benjamin_Choi 2021. 7. 4.

https://leetcode.com/problems/container-with-most-water/

/**
 * @param {number[]} height
 * @return {number}
 */
 var maxArea = function(height) {
    var p1 = 0, p2 = height.length - 1;
    var max = {area: 0, height: 0};
    while(p1 !== p2) {
        var minHeight = Math.min(height[p1], height[p2]);
        var width = p2 - p1;
        if (minHeight > max.height) {
            var area = minHeight * width;
            if (max.area < area) {
                max.area = area;
                max.height = minHeight;
            }    
        }
        if (minHeight === height[p1]) {
            p1++;
        } else {
            p2--;
        }
    }
    return max.area;
};

댓글