这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =)

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) {
}
};

题目不难,观察下就可以发现被水填满后的形状是先升后降的塔形,因此,先遍历一遍找到塔顶,然后分别从两边开始,往塔顶所在位置遍历,水位只会增高不会减小,且一直和最近遇到的最大高度持平,这样知道了实时水位,就可以边遍历边计算面积。

从看题目开始一共十分钟,算本菜鸟在AC率二字打头的题目中至今最快的一次。。

class Solution {
public:
int trap(int A[], int n) {
if(n <= ) return ;
int max = -, maxInd = ;
int i = ;
for(; i < n; ++i){
if(A[i] > max){
max = A[i];
maxInd = i;
}
}
int area = , root = A[];
for(i = ; i < maxInd; ++i){
if(root < A[i]) root = A[i];
else area += (root - A[i]);
}
for(i = n-, root = A[n-]; i > maxInd; --i){
if(root < A[i]) root = A[i];
else area += (root - A[i]);
}
return area;
}
};

总结:

这道题和LeetCode上 "Candy" 一题都采用了定义两个指针向中部某一点靠拢的做法(当然Candy还有更快的解,见以前的这篇),这也算是小技巧之一吧,在需要时要能第一时间想到。

[LeetCode] 接雨水,题 Trapping Rain Water的更多相关文章

  1. leetcode 第41题 Trapping Rain Water

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

  2. [Swift]LeetCode407. 接雨水 II | Trapping Rain Water II

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

  3. LeetCode(42)Trapping Rain Water

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

  4. LeetCode 42. 接雨水(Trapping Rain Water)

    题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况 ...

  5. LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]

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

  6. LeetCode: Trapping Rain Water 解题报告

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

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

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

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

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

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

随机推荐

  1. Cassandra 类型转换限制

    原文地址:http://stackoverflow.com/questions/31880381/cassandra-alter-column-type-which-types-are-compati ...

  2. 上层应用与wpa_supplicant,wpa_supplicant与kernel 相关socket创建交互分析

    单独拿出来,分析以下上层应用与wpa_supplicant   wpa_supplicant与kernel 的socket交互. 关联上层应用与wpa_supplicant的socket的创建.连接流 ...

  3. return阻止js继续向下执行

    终止JS运行有如下几种可能: 终止函数的运行的方式有两种 在函数中使用return,则当遇到return时,函数终止执行,控制权继续向下运行 在函数中使用try-catch异常处理,需要结束时,使用t ...

  4. 2019寒假训练营寒假作业(三) 对Sketch——CM-Sketch的理解(理论题部分)

    目录 实验题部分 基本题 1.简述sketch: 2.Count-min Sketch: 开放题部分 理论部分 1.解释为什么 sketch 可以省空间 2.用流程图描述Count-min sketc ...

  5. LintCode-5.第k大元素

    第k大元素 在数组中找到第k大的元素 注意事项 你可以交换数组中的元素的位置 样例 给出数组 [9,3,2,4,8],第三大的元素是 4 给出数组 [1,2,3,4,5],第一大的元素是 5,第二大的 ...

  6. iOS开发热更新JSPatch

    JSPatch,只需在项目中引入极小的引擎,就可以使用JavaScript调用任何Objective-C的原生接口,获得脚本语言的能力:动态更新APP,替换项目原生代码修复bug. 是否有过这样的经历 ...

  7. MDK中的调试脚本分析

    准备写一个简单的裸机程序放mini2440里面跑,看到芯片手册有2中启动方式,1.从Nor Flash启动 2.从Nand Flash中启动.断断续续弄了几天始终无法烧录进去,想起平时自己工作中调试程 ...

  8. Thinkphp5的ajax接口实现

    前一篇讲到thinkphp5从数据库获取数据之后赋给视图view,前一篇从数据渲染方式来说是服务端数据渲染,这一章则是浏览器端数据渲染.按照知识总结依据来划分,这是两种不同的技术场景. 下面介绍具体的 ...

  9. intellij idea 如何将一个普通项目转换为maven项目

    1.工程文件下新建文件pom.xml,并填写好内容. 2.在pom.xml 文件上右键 Add as Maven Project.

  10. Apriori算法详解

    一.Apriori 算法概述Apriori 算法是一种最有影响力的挖掘布尔关联规则的频繁项集的 算法,它是由Rakesh Agrawal 和RamakrishnanSkrikant 提出的.它使用一种 ...