题目链接: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. SecureCRT & SecureFx 绿色破解版

    租了腾讯云的服务器,是 ubuntu 版的,需要用到 SecureCRT 工具来远程链接,但是连接的只是控制台,只能输入命令操作,如果要下载文件什么的,就很麻烦,这时可以使用 SecureFx 来传输 ...

  2. python urlib2报错gaierror: [Errno 11004] getaddrinfo failed

    gaierror : get address info error,获取网络地址信息错误. url不正确,代理信息配置不正确都会报这个错误. 摘自https://blog.csdn.net/qq_19 ...

  3. nginx 1.14.0 配置部署 thinkphp 5.1

    开始接触NGINX,配置tp5配了半天,找不到具体原因,于是用网上搜索到的配置复制粘贴搞定. 感谢 https://blog.csdn.net/qq_36431213/article/details/ ...

  4. Mybatis 系列9-强大的动态sql 语句

    [Mybatis 系列10-结合源码解析mybatis 执行流程] [Mybatis 系列9-强大的动态sql 语句] [Mybatis 系列8-结合源码解析select.resultMap的用法] ...

  5. Calling async method synchronously

    https://stackoverflow.com/questions/22628087/calling-async-method-synchronously/22629216#22629216 ht ...

  6. SAS 报表输出一些新式控制

    SAS 报表输出一些新式控制 *******************************:*Purpose: 报表*Programm: *Programmor: *Date: *Version: ...

  7. java中序列化的简单认识

    一.什么是序列化 Java平台允许我们在内存中创建可复用的Java对象,但一般情况下,只有当JVM处于运行时,这些对象才可能存在,即,这些对象的生命周期不会比JVM的生命周期更长.但在现实应用中,就可 ...

  8. CentOS7.4安装部署openstack [Liberty版] (二)

    继上一篇博客CentOS7.4安装部署openstack [Liberty版] (一),本篇继续讲述后续部分的内容 一.添加块设备存储服务 1.服务简述: OpenStack块存储服务为实例提供块存储 ...

  9. RecyclerView添加条目点击事件setOnItemClickListener,不是在Adapter中设置;

    RecyclerView不像ListView,可以直接写setOnItemClickListener,我们大部分都是在Adapter中的设置点击事件,这个是使用RecyclerView的addOnIt ...

  10. java类库 collection与collections (转)

    http://www.cnblogs.com/dashi/p/3597937.html Java中Collection和Collections的区别 1.java.util.Collection 是一 ...