【LeetCode每天一题】Trapping Rain Water(获得雨水的容量)
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.Thanks Marcos for contributing this image!
Example:
Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 思路
在面对这道题时,我们可以设置两个指针一直指向右边,一个指向左边。基础我们每次让值小的一边移动,并且保存两边中各自最高的高度。每一次都判断和各自边上的高度之差,并得到可以蓄水的容量。直到两个节点相遇。完成遍历。时间复杂度为O(n),空间复杂度为O(1)。 图示步骤
解决代码
class Solution(object):
def trap(self, nums):
"""
:type height: List[int]
:rtype: int
"""
if len(nums) < 3: # 当数组的长度小于3时,不能蓄水直接返回结果。
return 0
max_left, max_right, left, right = nums[0],nums[-1], 0, len(nums)-1 # 设置两边的最高节点值,和左右两个指针。
res_water = 0 # 蓄水量
while left < right:
if nums[left] < nums[right]: # 我们先判断当前左右两节点大小,来决定来计算那一边。小的那一边开始(如果从大的一边开始会出现错误)
if nums[left] >= max_left: # 在判断 左边节点和左边最高的节点之间的大小关系
max_left = nums[left] # 当当前的左边节点大于或者等于之前左边最高节点值时,我们重新进行赋值。
else:
res_water += max_left - nums[left] # 否则可以蓄水,加上高度差,即为蓄水量
left += 1 # 向前移动一位
else:
if nums[right] >= max_right: # 和上边的同理
max_right = nums[right]
else:
res_water += max_right - nums[right]
right -= 1
return res_water # 循环结束返回结果。
【LeetCode每天一题】Trapping Rain Water(获得雨水的容量)的更多相关文章
- leetcode 第41题 Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- [LeetCode] 接雨水,题 Trapping Rain Water
这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LintCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 【LeetCode】42. Trapping Rain Water 接雨水 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...
- LeetCode(42)Trapping Rain Water
题目 Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...
- LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]
题目:Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...
- 042 Trapping Rain Water 接雨水
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算下雨之后能接多少雨水.例如,输入 [0,1,0,2,1,0,1,3,2,1,2,1],返回 6. 详见:https://leetcode.c ...
随机推荐
- EF中的预先加载和延迟加载
延迟加载(Lazy Loading):当实体第一次被读取时,相关数据不会被获取,只会读取本身.延迟加载的数据不会一次性查出来,而是一条一条的查询,这样就会多次请求数据库进行查询. 预先加载<Ea ...
- [Asp.net]缓存简介
写在前面 针对一些经常访问而很少改变的数据,使用缓存,可以提高性能.缓存是一种用空间换取时间的技术,说的直白点就是,第一次访问从数据库中读取数据,然后将这些数据存在一个地方,比如内存,硬盘中,再次访问 ...
- Unable to cast object of type 'System.Int32' to type 'System.Array'.
x 入职了新公司.最近比较忙...一看博客...更新频率明显少了...罪过罪过... 新公司用ASP.NET MVC 遇上一个错误: Unable to cast object of type 'Sy ...
- ASP.NET与ASP.NET Core用户验证Cookie并存解决方案
在你将现有的用户登录(Sign In)站点从ASP.NET迁移至ASP.NET Core时,你将面临这样一个问题——如何让ASP.NET与ASP.NET Core用户验证Cookie并存,让ASP.N ...
- ESP8266 的几个代码 加深对LUA的理解
--some functions dofile("functions.lua") lighton1= lighton2= lighton3= lighton4= pin1 = pi ...
- 关于ti环境搭建 IAR 为什么IAR不能跳转的真正原因
1.IAR工程莫名其妙的错误,可以通过复制工程文件来解决 2.cc2540环境配置,尽量默认到c盘,以便解决不必要的麻烦,另外,不同的库尽量安装对应的环境文件,比如freertos等环境 3.对于蓝牙 ...
- 1.7Oob 成员变量可以不初始化 但局部变量必须初始化
成员变量有默认的初始值 像int a; a默认为0:而局部变量没有初始值
- Srt字幕文件解析
// // ViewController.m // 字幕解析 // // Created by admin on 2018/8/30. // Copyright © 2018年 admin. All ...
- [developmemt][dpdk] dpdk优化(转)
转发:https://software.intel.com/en-us/articles/dpdk-performance-optimization-guidelines-white-paper 转发 ...
- Copycat - Overview
Copycat’s primary role is as a framework for building highly consistent, fault-tolerant replicated s ...
