算法:42.接雨水

/**
 * @param {number[]} height
 * @return {number}
 */
var trap = function(height) {
  let n=height.length;
  if(n===0) return 0;
  let res=0;

  let left_max=[] ,right_max=[];
  //记录左边数组的最大值
  left_max[0]=height[0];
  for(let i=1;i<n;i++){
    left_max[i]=Math.max(left_max[i-1],height[i]);
  }
  //记录右边数组的最大值
  right_max[n-1]=height[n-1];
  for(let i=n-2;i>=0;i--){
    right_max[i]=Math.max(right_max[i+1],height[i]);
  }
  //统计每一列的面积之和
  for(let i=0;i<n;i++){
    res+=Math.min(left_max[i],right_max[i])-height[i];
  }
  return res;
};

时间复杂度:O(n)。

存储最大高度数组,需要两次遍历,每次 O(n) 。
最终使用存储的数据更新ans ,O(n)。
空间复杂度:O(n) 额外空间。

原文地址:https://www.cnblogs.com/xulei1992/p/14335432.html