11. Container With Most Water

2021-01-21  本文已影响0人  jluemmmm

容器中可以最大装水数,双指针法进行实现

/**
 * @param {number[]} height
 * @return {number}
 */
var maxArea = function(height) {
    let start = 0
    let end = height.length - 1
    let cur = 0
    let max = 0
    while (start < end) {
        if(height[start] > height[end]) {
            cur = (end - start) * height[end]
            end--
        } else {
            cur = (end - start) * height[start]
            start++
        }
        if(cur > max) max = cur
    }
    return max
};
上一篇下一篇

猜你喜欢

热点阅读