用简单思维解决LeetCode中困难级别的题 —— 接雨水问题

之前看面试题的时候,看到了一个接雨水的问题,和小黄鸭讨论之后,觉得很有趣呢,这里和大家分享一下这个解法。后来看到LeetCode上面有这道题,题号42,有兴趣的可以做一下。

问题描述

给定n个非负整数表示每个宽度为1的柱子的高度图,计算彼此排列的柱子,下雨之后能接多少雨水。

示例1:

输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6

实例2:

输入:height = [4,2,0,3,2,5]
输出:9

你能不能先思考一下,遇到这种问题,你要怎么做呢?

问题分析

首先我们可以根据给的数组,先画出来柱子的长度。

柱子长度.png

那么我们怎么确定接的雨水的量呢?当然是两个都高中间低的地方,来存储水,下面阴影的部分就是储存水的位置。

雨水部分.png

那么我们需要对左边和右边最高水位做一个统计,这边使用到了两个辅助数组

一个用来储存左边的最大接雨水数,一个存储右边的最大接雨水数。

最大接雨水数.png

选择两个中最小的那个作为存储水的量

QQ图片20210421181221.png

当然还要减去自己柱子的高度。剩下的,就是可以接的雨水了。

QQ图片20210421181257.png

代码整理

function trap(height) {
    if(!height.length) return 0;
    let left = []
    let right = []
    let lmax = rmax = 0
    let len = height.length
    let result = 0
    // 把左右最大出水量求出来
    for(let i = 0; i < len; i++) {
        lmax = Math.max(height[i], lmax)
        rmax = Math.max(height[len-i-1], rmax)
        left[i] = lmax
        right[len- i - 1] = rmax
    }
    // 算出最小的然后累加
    for(let i = 0; i < len; i++) {
        result += Math.min(left[i],right[i]) - height[i]
    }
    return result
};

优化分析

  • 时间复杂度O(n)
  • 空间复杂度O(n)

如果想要写法上优化一下,可以第二次遍历的时候可以使用reduce

function trap(height) {
    if(!height.length) return 0;
    let left = []
    let right = []
    let lmax = rmax = 0
    let len = height.length
    for(let i = 0; i < len; i++) {
        lmax = Math.max(height[i], lmax)
        rmax = Math.max(height[len-i-1], rmax)
        left[i] = lmax
        right[len- i - 1] = rmax
    }
    return height.reduce((prev, item, index) => {
        return prev + Math.min(left[index],right[index]) - item
    }, 0)
};

其实右边数组的值的存储是可以省略的,虽然都是空间复杂度O(n),但是也算小小的优化点。

function trap(height) {
    if(!height.length) return 0;
    let left = []
    let lmax = rmax = 0
    let len = height.length
    let result = 0
    for(let i = 0; i < len; i++) {
        lmax = Math.max(height[i], lmax)
        left[i] = lmax
    }
    // 从后往前遍历
    for(let i = len - 1; i >= 0; i--) {
        rmax = Math.max(height[i], rmax)
        result += Math.min(left[i],rmax) - height[i]
    }
    return result
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值