题目链接:https://leetcode.com/problems/trapping-rain-water/description/

思路:

1、先找到最左侧第一个高度不为0的柱子i。

2、从i+1开始向右侧找另一个柱子max。条件:要么a[max] > a[i],要么a[max] > 右侧所有。

1) 向右侧遍历,当遇到第一个比a[i]高的柱子,相当于遇到一座山,这个柱子会把左右两侧的池子给隔开。

所以,如果遇到第一个比a[i]高的柱子,就停下来。这时候就产生了一个和右侧隔开的池子。

2) 如果找到最后,一直没有遇到比a[i]还高的,那就取右侧最高的那个作为max。

a[max] 和 a[i]组成一个和右侧隔开的池子。

池子的面积就是 min(a[max] , a[i]) * (max - i - 1) - sum(a[i+1]...a[max-1])

3、把i定位到max,重复上述过程。以max为起点,找下一个池子。

show me the code:

class Solution {
public:
int trap(vector<int>& height) {
vector<int> &a = height;
int i,n = a.size();
int sum = ;
for(i = ;i < n - ; )
{
while(i < n - && a[i] == ) ++i; int max = i + ; //i右侧最大值的下标 for(int j = i+; j < n; j++)
{
if(a[j] > a[max]) max = j;
if(a[max] > a[i]) break;
} sum += min(a[i],a[max])*(max - i - ); for(int j = i+; j< max; j++)
sum -= a[j]; i = max;
} return sum;
}
};

Leetcode 题解 Trapping Rain Water的更多相关文章

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

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

  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 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

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

  4. [LeetCode] 407. Trapping Rain Water II 收集雨水 II

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

  5. [LeetCode] 42. Trapping Rain Water 收集雨水

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

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

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

  7. LeetCode - 42. Trapping Rain Water

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

  8. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  9. LeetCode 042 Trapping Rain Water

    题目要求:Trapping Rain Water Given n non-negative integers representing an elevation map where the width ...

随机推荐

  1. Oracle之ora-01031 insufficient privileges

              解决ora-01031insufficient privileges错误 解决system用户不能登录的问题 alter user system account unlock id ...

  2. win server 2008 R2 支持

    安装Microsoft Visual C++ 2010 可再发行组件包 (x64) vcredist_x64.exe 安装Microsoft .NET Framework 4 dotNetFx40_F ...

  3. [UE4]碰撞的随机性

    物理引擎(包括碰撞)的计算具有随机性 原因: 一.每一帧的时间并不是严格相等 二.浮点数计算不是完全准确(两个浮点数运算,结果不可重复) 影响 在左边窗口(服务器端)打几发子弹把其中3个立方体的位置打 ...

  4. [UE4]控件模板参数

    创建的时候就会变成这样了.

  5. GDAL 地图切片层级计算公式

    作者: 蔡建良 2016-7-6 地图瓦片起始层数: xMin=栅格数据最小经度 xMax=栅格数据最大经度 起始层数=Log(第0层经纬度跨度/当前地图的经纬度跨度,2) minzoom = (in ...

  6. (转)MTU&MSS

    MTU是Maximum Transmission Unit的缩写,意为最大传输单元,通俗的理解就是在网络上传送的最大数据包,单位是字节. 以太网对数据帧的长度都有一个限制,其最大值为1500,这个特性 ...

  7. Spring IoC中各个注解的理解和使用

    一.把在Spring的xml文件中配置bean改为Spring的注解来配置bean 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的 ...

  8. Delphi SetParent 嵌入其他应用程序

    [代码]Delphi实现窗体内嵌其他应用程序窗体 实现原理是启动一个应用程序,通过ProcessID得到窗体句柄,然后对其设定父窗体句柄为本程序某控件句柄(本例是窗体内一个Panel的句柄),这样就达 ...

  9. 《机器学习实战》ID3算法实现

    注释:之前从未接触过决策树,直接上手对着书看源码,有点难,确实有点难-- 本代码是基于ID3编写,之后的ID4.5和CART等还没学习到 一.决策树的原理 没有看网上原理,直接看源码懂得原理,下面是我 ...

  10. Matplotlib模块

    不求甚解,不断学习不断添加... 2017.10.26 1.绘制简单的图像 # 第一步创建显示画面,figure('show')指定图表名称 plt.figure('data') #绘制图像--> ...