42

暴力解法,时间复杂度O(n^2),空间复杂度O(1)

var trap = function(height) {
    var res = 0
    for(var i = 1; i < height.length - 1; i++){
        var l = 0
        var r = 0
        for(var j = i; j >= 0; j--){
            l = Math.max(l, height[j])
        }
        for(var j = i; j < height.length; j++){
            r = Math.max(r, height[j])
        }
        res = res + Math.min(l, r) - height[i]
    }
    return res
};

动态规划

在这道题的算法就是,在找左边第i个最大元素的时候,要用到第i-1次的结果,时间复杂度O(n),空间复杂度O(n)

var trap = function(height) {
    var res = 0
    var left = []
    var right = []
    var n = height.length 
    left[0] = height[0]
    right[n - 1] = height[n - 1]
    for(var i = 1; i < height.length; i++){
        left[i] = Math.max(left[i - 1], height[i])
    }
    
    for(var i = n - 2; i >= 0; i--){
        right[i] = Math.max(right[i + 1], height[i])
    }
    for(var i = 1; i < n - 1; i++){
        res += Math.min(left[i], right[i]) - height[i]
    }
    return res
};

双指针法

时间复杂度O(n),空间复杂度O(1),faster than 99.8%

var trap = function(height) {
    var n = height.length
    var left = 0
    var right = n - 1
    var l_max = 0
    var r_max = 0
    var res = 0
    while(left < right){
        if(height[left] <= height[right]){
            if(height[left] >= l_max){
                l_max = height[left]
            }else{
                res += l_max - height[left]
            }
            left++
        }else{
            if(height[right] >= r_max){
                    r_max = height[right]
            }else{
                res += r_max - height[right]
            }
            right--
        }
    }
    return res
};

你可能感兴趣的:(42)