rottenpen / blog

日常记录 blog,内容不限于前端,博文在 issue https://github.com/rottenpen/blog/issues
7 stars 0 forks source link

接雨水 #17

Closed rottenpen closed 3 years ago

rottenpen commented 5 years ago

失败方案1:

var trap = function(height) {
    let sum = 0
    let max = 0
    let num = 0
    let newList = [...new Set(height)].sort((a,b) => a - b)
    let lastArr
    let lastnum = 0
    max = newList[newList.length - 1]
    while (newList[num] <= max) {
        lastArr = lastArr ? getNewArr(lastArr, newList[num]) : getNewArr(height, newList[num])
        lastnum = num > 0 ? newList[num - 1] : 0
        lastArr.forEach((ele) => {
            let cut = ele - lastnum > 0 ? ele : lastnum
            let add = newList[num] - ele > 0 ?  newList[num] - cut : 0
            // console.log(getNewArr(height, newList[num]), add)
            sum = sum + add
        })
        num ++ 
    }
    return sum
};
var getNewArr = function(arr = [], height = 1,) {
    let l = 0
    let r = arr.length - 1
    while (l <= r) {
      if (arr[l] >= height && arr[r] >= height) { arr = arr.slice(l, r + 1); break}
      if (arr[l] < height) { l++ }
      if (arr[r] < height) { r-- } 
    }
    return arr
}

失败方案2:

var trap = function(height) {
  if(height.length === 0){ return  0}
  let sum = 0 // 结果
  let newList = [...new Set(height)].sort((a,b) => a - b) // 升序的数组
  max = newList[newList.length - 1] 
  let map = getNewArr(height, newList)
  sum = getSum(newList, map, height)
  return sum
};
function getSum(newList, map, arr, lastnum = 0) {
  var len = newList.length;
  let lr = map[newList[0]]
  let sum = 0
  lastArr = arr.slice(lr[0], lr[1] + 1) // 获取过滤后的数组
  // lastnum = num > 0 ? newList[num - 1] : 0 // 上一轮的高度 如果等于num等于0 那么上一次的高度应该是 0 
  lastArr.forEach((ele) => {
      let cut = ele - lastnum > 0 ? ele : lastnum
      let add = newList[0] - ele > 0 ?  newList[0] - cut : 0
      sum = sum + add
  })
  if(len == 0){
      return 0;
  } else if (len == 1){
      return sum;
  } else {
      return sum + getSum(newList.slice(1), map, arr, newList[0]);
  }
}
var getNewArr = function(arr = [], sort) { //过滤掉左右不需要的值 获得一个高度的映射
  let obj = {}
  let l = 0
  let r = arr.length - 1
  let num = 0
  while (l <= r) {
    if (arr[l] >= sort[num] && arr[r] >= sort[num]) { obj[sort[num]] = [l, r] ; num++; if(num === sort.length) break }
    if (arr[l] < sort[num]) { l++ }
    if (arr[r] < sort[num]) { r-- }
  }
  return obj
}
rottenpen commented 5 years ago
/**
 * @param {number[]} height
 * @return {number}
 */
var trap = function(height) {
    let max = Math.max(...height)
    // let maxIndexs = []
    let l = 0
    let r = height.length - 1
    let sum = 0
    let maxIndex = height.indexOf(max)
    let lastLeft = 0
    let lastRight = 0
    // console.log(maxIndex)
    if(!height.length){
        return 0
    } else {
        while (l<r) {
            if(l !== maxIndex) {
                let ll = l ? height[l - 1] : 0
                lastLeft = lastLeft < ll ? ll: lastLeft
                let diffLeft =  lastLeft - height[l] // 左边高于下一个
                // console.log("l" + ll, lastLeft, diffLeft)
                sum = diffLeft > 0 ? sum + diffLeft : sum
                l ++;
            }
            if(r !== maxIndex) {
                let rr = r < height.length - 1 ? height[r + 1] : 0
                lastRight = lastRight < rr ? rr: lastRight
                let diffRight =  lastRight - height[r] // 左边高于下一个
                // console.log("r" + rr,r, lastRight, diffRight)
                sum = diffRight > 0 ? sum + diffRight : sum
                r --;
            }
        }
    }
    return sum  
};

终于成了