原题地址:https://oj.leetcode.com/problems/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.

For example, 
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

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!

解题思路:模拟法。开辟一个数组leftmosthigh,leftmosthigh[i]为A[i]之前的最高的bar值,然后从后面开始遍历,用rightmax来记录从后向前遍历遇到的最大bar值,那么min(leftmosthigh[i], rightmax)-A[i]就是在第i个bar可以储存的水量。例如当i=9时,此时leftmosthigh[9]=3,而rightmax=2,则储水量为2-1=1,依次类推即可。这种方法还是很巧妙的。时间复杂度为O(N)。

代码:

class Solution:
# @param A, a list of integers
# @return an integer
def trap(self, A):
leftmosthigh = [0 for i in range(len(A))]
leftmax = 0
for i in range(len(A)):
if A[i] > leftmax: leftmax = A[i]
leftmosthigh[i] = leftmax
sum = 0
rightmax = 0
for i in reversed(range(len(A))):
if A[i] > rightmax: rightmax = A[i]
if min(rightmax, leftmosthigh[i]) > A[i]:
sum += min(rightmax, leftmosthigh[i]) - A[i]
return sum

[leetcode]Trapping Rain Water @ Python的更多相关文章

  1. [LeetCode] Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  2. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  3. LeetCode: Trapping Rain Water 解题报告

    https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...

  4. Leetcode: Trapping Rain Water II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  5. Leetcode Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  6. [LeetCode] Trapping Rain Water 栈

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  7. [LeetCode] Trapping Rain Water II 题解

    题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...

  8. leetcode Trapping Rain Water pthon

    class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...

  9. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

    LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...

随机推荐

  1. 系统的Drawable(二)-Selector

    系统的Drawable(二)-Selector Selector漫谈 Selector是定义 StateListDrawable 的标签,该Drawable代表着一个Drawable的集合,每一个Dr ...

  2. 隐马尔科夫模型(HMM)与词性标注问题

    一.马尔科夫过程: 在已知目前状态(现在)的条件下,它未来的演变(将来)不依赖于它以往的演变 (过去 ).例如森林中动物头数的变化构成——马尔可夫过程.在现实世界中,有很多过程都是马尔可夫过程,如液体 ...

  3. Ubuntu 安装Chrome

    apt方式安装Chrome 1.添加密匙 wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key a ...

  4. 一键安装LNMP/LAMP

    安装步骤:1.使用putty或类似的SSH工具登陆VPS或服务器: 登陆后运行:yum install screen安装  screen screen -S lnmp创建一个名字为lnmp的会话 2. ...

  5. 用户 'IIS APPPOOL\DefaultAppPool' 登录失败【收藏】

    转载:http://blog.csdn.net/wenjie315130552/article/details/7246143 问题是应用程序连接池的问题.网上有些朋友说是Temp文件夹的权限的问题. ...

  6. C++ STL set::find的用法

      参考: http://blog.csdn.net/lihao21/article/details/6302196 /* class for function predicate * - opera ...

  7. 8张图理解Java---importnew---programcreek

    http://www.importnew.com/11725.html https://www.programcreek.com/2013/09/top-8-diagrams-for-understa ...

  8. mysql主从同步失败Last_IO_Error: Got fatal error 1236 from master解决方法

    mysql教程主从同步失败Last_IO_Error: Got fatal error 1236 from master解决方法 遇到这样的错误如:“Last_IO_Error: Got fatal ...

  9. Ubuntu上的Hadoop安装教程

    Install Hadoop 2.2.0 on Ubuntu Linux 13.04 (Single-Node Cluster) This tutorial explains how to insta ...

  10. 在Visual Studio中使用序列图描述对象之间的互动

    当需要描述多个对象之间的互动,可以考虑使用序列图. 在建模项目下添加一个名称为"Basic Flow"的序列图. 比如描述客户是如何在MVC下获取到视图信息的. 备注: ● 通常是 ...