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!

题意分析:

  给定一个非负整数的list,每一个元素代表如图中柱状图的纵坐标。结合图中来看,黑色的是给定的list,让你求出这些黑色柱体围起来能够盛的水量是多少,也就是图中蓝色柱体的面积(每个柱体宽都是1)。

解答:

  本题和11. Container With Most Water有些类似,那道题求的是两个坐标之间能够盛的最大水量,本题是所有坐标中能够盛下的水量。考虑到每个坐标宽度都是1,所以我们把问题简化为求每个坐标上面能够盛的水量。

  那么单个坐标的水量怎么求呢?我们假设i为当前需要求的坐标,如果i左侧比i高且是最高的设为left_max,同时能够保证i右侧有比left_max高的坐标,那么i能够盛的水量就是left_max减去i。基于这样一种思路,我们依然可以用双指针的方式去解。

AC代码:

class Solution(object):
def trap(self, height):
ret_num = 0
left, right, left_max, right_max = 0, len(height) - 1, 0, 0
while left <= right:
if height[left] < height[right]:
if height[left] >= left_max:
left_max = height[left]
else:
ret_num += left_max - height[left]
left += 1
else:
if height[right] >= right_max:
right_max = height[right]
else:
ret_num += right_max - height[right]
right -= 1
return ret_num

【LeetCode题意分析&解答】42. Trapping Rain Water的更多相关文章

  1. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

  2. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  3. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

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

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

  5. LeetCode - 42. Trapping Rain Water

    42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...

  6. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

  7. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  8. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  9. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  10. 刷题42. Trapping Rain Water

    一.题目说明 题目是42. Trapping Rain Water,翻译起来就是"接雨水".给n个非负正数代表高度,每个正数宽度为1,让计算能多少雨水.题目难度是Hard 二.我的 ...

随机推荐

  1. Excal数据转化成Asset数据文件

    我们知道,在Unity当中的文件都可以称之为Asset文件,在项目开发当中需要把数据读取来之后存放起来,而有的数据是不可以改变的,今天就来写一个demo处理一下这些数据,在这里就不写读取Excal数据 ...

  2. css 中的若干心得

    css布局中定位机制主要是普通的流,也就是说按照HTML文本的顺序在窗口上从上到下.从左到右去显示,遇见块级元素就换行显示.为了更进一步的控制,我可以使用相对定位.绝对定位.固定定位以及浮动. 相对定 ...

  3. shell 比较

    整数比较 -eq 等于,如:if [ "$a" -eq "$b" ] -ne 不等于,如:if [ "$a" -ne "$b&qu ...

  4. Windows下C++多线程同步与互斥简单运用

    1.  互斥量,Mutex #include <Windows.h> #include <iostream> using namespace std; DWORD WINAPI ...

  5. HTTP POST和GET的区别[转]

    http://www.cppblog.com/woaidongmao/archive/2008/05/29/51476.aspx 1.HTTP 只有POST和GET 两种命令模式: 2.POST是被设 ...

  6. Android 通过HTTPCLINET GET请求互联网数据

    private EditText et; private TextView tv; HttpClient client; @Override protected void onCreate(Bundl ...

  7. [转载]node.js express 4.x 安装指南,没有自动配置环境变量的问题

    前几天express 推出了4.0,得知这个消息,自己尝试了一下,突然发现用以前的文档上的操作出现了各种问题.结果只能去看文档,现在在这个给大家分享下4.0版本的安装. 先说下如果需要用express ...

  8. CSS自学笔记(7):CSS定位

    很多时候,我们需要对一些元素进行自定义排序.布局等,这是就需要用到CSS的定位属性了,用这些属性对一些元素进行自定义排序.布局等操作,可以改变浏览器默认的死板的排序. CSS定位的功能很容易理解,它允 ...

  9. windbg命令学习4

    4.查看调用栈 k命令:显示的是一定数量的栈帧, 其中帧的数量是由.kframes命令来控制的, 默认值是256. 我们如何来判断函数的栈指针,参数地址和局部变量地址呢? 举一个简单的windbg的k ...

  10. android 环境搭建 windows, linux

    android环境也搭建了很多次了,linux下window下.在这里记录下,以后再搭建设置变量啥的就直接看自己的博客就好了.电子挡笔记有时候也不方便 1.下载材料 概述:用的是比较简单的方式搭建环境 ...