Leetcode 11-the container with the most water

/**
* @param {number[]} height
* @return {number}
* 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/container-with-most-water
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/

const arr = [1,8,6,2,5,4,8,3,7]
function getMaxArea(height) {
    
    
    let i= 0; j = height.length-1
    let max = 0; cur_area = 0
    while(i < j) {
    
    
        if(height[i] > height[j]){
    
    
            cur_area = height[j] * (j -i)
            j --
        }else {
    
    
            cur_area = height[i] * (j -i)
            i ++
        }
        max = Math.max(cur_area, max)
    }
    return max 
}

const ans = getMaxArea(arr)
console.log(ans);


Guess you like

Origin blog.csdn.net/weixin_40944062/article/details/113105017