题目要求: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!

代码如下:

class Solution {
public:
int trap(int A[], int n) { int maxIdx = 0;
int water = 0; //找到最长的木板,设为maxIdx
for(int i = 1; i < n; i++){
if(A[i] > A[maxIdx]){
maxIdx = i;
}
} int max = A[0]; //左侧逼近
for(int i = 1; i < maxIdx; i++){ if(max < A[i])
max = A[i];
//木板左边(max)和右边(最高)都比它高,则可以放
// max - 该木板长度 的水
else
water += max - A[i];
} //右侧逼近
max = A[n - 1];
for(int i = n - 2; i >= maxIdx; i--){
if(max < A[i]) max = A[i];
else water += max - A[i];
} return water; }
};

LeetCode 042 Trapping Rain Water的更多相关文章

  1. [leetcode][042] Trapping Rain Water (Java)

    我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目在这里: ...

  2. Java for LeetCode 042 Trapping Rain Water

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

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

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

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

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

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

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

  6. [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 ...

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

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

  8. [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 ...

  9. LeetCode - 42. Trapping Rain Water

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

随机推荐

  1. IDEA通过file-open打开以前的项目无法运行

    在学习java的过程中我们会建立很多项目,IDEA默认打开最近一次编辑的项目,当我们打开以前的项目时会发现run图标变成灰色了, 解决办法: 1. 手动设置src为根目录 选中src目录--右键--m ...

  2. http twisted

    Sunday, September 30th, 2007 Twisted的WEB开发 作者: gashero <harry.python@gmail.com> 目录 1   简介 2    ...

  3. ubuntu 17.10 安装QQ

    折腾一大堆 看报错信息 正在选中未选择的软件包 wine-qqintl:i386.(正在读取数据库 ... 系统当前共安装有 185429 个文件和目录.)正准备解包 wine-qqintl_0.1. ...

  4. opencv--ORB::create

    static Ptr<ORB> cv::ORB::create (                         int nfeatures = 500,                 ...

  5. leetcode6:binary-tree-postorder-traversal

    题目描述 求给定的二叉树的后序遍历. 例如: 给定的二叉树为{1,#,2,3}, 1↵ ↵ 2↵ /↵ 3↵ 返回[3,2,1]. 备注:用递归来解这道题太没有新意了,可以给出迭代的解法么? Give ...

  6. TRUNCATE 有约束的表

    在有外键约束的情况下,truncate 表时,会报错, 我们可以设置外键检测为flase,执行完truncate命令后,再启用 SET foreign_key_checks = 0;TRUNCATE ...

  7. 调度器简介,以及Linux的调度策略(转)

    进程是操作系统虚拟出来的概念,用来组织计算机中的任务.但随着进程被赋予越来越多的任务,进程好像有了真实的生命,它从诞生就随着CPU时间执行,直到最终消失.不过,进程的生命都得到了操作系统内核的关照.就 ...

  8. 多项目部署在同一个GitHub Pages

    由于GitHub 的约定,一个账户只能拥有一个GitHub Pages,那么,如果你有多个想部署的静态网站(博客和文档等),它们是互相隔离的,如何用同一个GitHub账户进行部署呢? 从之前如何搭建G ...

  9. TypeError: Cannot read property 'Component' of undefined

    继续跟着阮一峰的教程走,下面写到PropTypes的getDefaultProps时,又出现了问题,基于上一个createClass的报错换成了Component写法 错误描述: 解决方法:引入rea ...

  10. history命令的优化

    前言 默认的history记录的信息有限,我们对这个进行一定的扩充 我们看下大概哪几个需求 记录用户登陆的ip 记录用户的名称 记录执行命令的时间 具体实现 我们看下应该怎么做这个,尽量在不改变用户的 ...