[LC] 42. 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
class Solution(object):
def trap(self, height):
"""
:type height: List[int]
:rtype: int
"""
left, right = 0, len(height) - 1
leftMax, rightMax, res = 0, 0, 0
while left < right:
if height[left] <= height[right]:
leftMax = max(leftMax, height[left])
res += leftMax - height[left]
left += 1
else:
rightMax = max(rightMax, height[right])
res += rightMax - height[right]
right -= 1
return res
[LC] 42. Trapping Rain Water的更多相关文章
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- [Leetcode][Python]42: Trapping Rain Water
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- 刷题42. Trapping Rain Water
一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的 ...
- [LeetCode] 42. 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
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
随机推荐
- symmetry methods for differential equations,exercise 1.4
tex文档: \documentclass[a4paper, 12pt]{article} % Font size (can be 10pt, 11pt or 12pt) and paper size ...
- lvm镜像卷
镜像能够分配物理分区的多个副本,从而提高数据的可用性.当某个磁盘发生故障并且其物理分区变为不可用时,你仍然可以访问可用磁盘上的镜像数据.LVM在逻辑卷内执行镜像. 系统版本 # cat /etc/ce ...
- spring-boot 如何加载rsources下面的自定义配置文件
spring-boot 如何加载resources下面的自定义配置文件 https://segmentfault.com/q/1010000006828771?_ea=1144561
- JavaScript mixins
mixin 是一个类,该类的方法被添加,混合进另外一个类.一个基础类会包含mixin类的方法而不是继承它.这样你就可以使用不同的mixin类来增加或者增强基础类的功能. 这编内容包含怎么样使用java ...
- teminal / console / shell
console从应用程序角度看的(控制台是管理员用的,唯一的) teminal从用户角度看的(终端是用户用的) 应用程序与console交互 用户与teminal交互 teminal可以不存在 tem ...
- Java--Json解析
普通Json {"code":"S0000", "describe":"数据正常返回", "result&qu ...
- Python判断一个字符串是否包含子串的几种方法
转自---http://blog.csdn.net/yl2isoft/article/details/52079960 1.使用成员操作符 in >>> s='nihao,shiji ...
- Python基础学习五
Python基础学习五 迭代 for x in 变量: 其中变量可以是字符串.列表.字典.集合. 当迭代字典时,通过字典的内置函数value()可以迭代出值:通过字典的内置函数items()可以迭代出 ...
- Python语言学习前提:Pycharm的使用
一.Pycharm的使用 1.点击Pycharm的图标 2.点击首页Create New Project > 在弹出的页面点击Pure Python 3.选择项目文件存放的位置,选择完成之后点击 ...
- [NOIP 2002普及组]产生数(floyd+高精度)
https://www.luogu.org/problem/P1037 题目描述 给出一个整数 n(n<1030) 和 k 个变换规则(k<=15). 规则: 一位数可变换成另一个一位数: ...