LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/

目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的部分

假设:积木宽度均为1

输入:各个积木的高度

输出:所有积木能容纳水的“面积”

思考过程

1. 逐一求积木的间隔似乎不太容易。特别对于图中3-7积木间的容积,如果可以先求底部(4-6间)的容积,当求解上层(3-7)的容积时,还需要做额外的处理,如减掉底部的高度。

2. 既然如此,可否先求出3-7间整体的容积,再将两积木之间的积木面积(4、5、6)减去,即可得这个区域的容积?

3. 那么问题转换成了,已知一个积木,如何寻找下一个积木,并计算这个两积木间的容积

4. 尝试一,寻找一个至少不比当前积木低的积木,作为下一个积木,如图例子为当前积木3,下一个积木7。那么之间的容积如何计算呢,简单,积木3的高度乘以两积木间的间隔宽度,再减去,积木3与积木7之间的积木(4、5、6)面积。

5. 问题:下次迭代从何开始?上一个例子,可以从积木7继续算起。然而如果找不到相应的积木呢,那么从积木3的下一个积木4开始。

6. 看起来,似乎正确。于是乎,开始写代码了。然而第一次提交就献给了WA(/(ㄒoㄒ)/~~)

7. 错误的情况,如果当前的积木是一个特别高的积木,后继找不到更高的积木,然而实际上之间是可能有容积的!不赘述了,补救思路是:在寻找更高的积木的同时,记录当前找到的最高的积木。如果没有找到更高的积木,使用当前找到的最高的积木作为下一个积木,并计算容积。

思路总结

迭代的过程

1. 寻找下一个至少不比当前积木低的积木,寻找的同时,记录当前找到的最高的积木高度

2. 如果找到,则计算两积木之间的容积(计算过程见思考过程2)

3. 如果没有找到,则计算当前积木与当前找到的最高的积木之间的容积

4. 当前积木设置为找到的积木(可能是2或3的情况),继续迭代

时间复杂度

O(n)

代码实现

 #include <iostream>
#include <vector> using namespace std; class Solution {
public:
int trap(vector<int>& height) {
if (height.size() == ) {
return ;
} int water = ;
int preIndex = ;
int preHeight = ; // 找到第一个高度非0的bar
while (preIndex < height.size() - && height[preIndex] == ) {
++preIndex;
} if (preIndex == height.size() - ) {
return ;
} // 遍历
while (preIndex < height.size()) {
preHeight = height[preIndex]; // 寻找更高的或相等的bar
// 同时记录遍历过的最高的bar
// 如果寻找不到更高的或相等的bar时,使用记录值计算
int next = preIndex + ;
int minHeight = ;
int minIndex = next; while (next < height.size() && height[next] < preHeight) {
if (height[next] > minHeight) {
minHeight = height[next];
minIndex = next;
}
++next;
} // 如果找到
if (next != height.size()) {
water += (preHeight * (next - preIndex - )); // 计算总面积 for (int i = preIndex + ; i < next; ++i) { // 减去中间bar面积
water -= height[i];
} preIndex = next; // 从找到的bar开始下一次的迭代
} else {
water += (minHeight * (minIndex - preIndex - )); // 计算总面积 for (int i = preIndex + ; i < minIndex; ++i) { // 减去中间bar面积
water -= height[i];
} preIndex = minIndex; // 从次高的bar开始
}
} return water;
}
}; int main(int argc, char const *argv[]) {
Solution solution;
vector<int> height = {,,,,,,,,,,,};
cout << solution.trap(height) << endl;
return ;
}

Trapping Rain Water

闲聊一二

大年初一!祝大家猴年快乐啦,已经工作的童鞋升职加薪,要实习、找工作的童鞋offer多多~博主今年也要面对找实习、找工作的人生大事儿了。好久没写博客了,默默的哀悼上个学期,自己瞎折腾,弄出的不成熟的东西。希望猴年是激情,奋斗,收获的一年~

ps:有没有对我感兴趣的boss收留~附交友地址一枚https://github.com/zrss

简单粗暴的resume:https://github.com/zrss/ghostblog,近期再修改下

有意思的数学题:Trapping Rain Water的更多相关文章

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

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

  2. [LeetCode] 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] Trapping Rain Water 收集雨水

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

  4. [LintCode] Trapping Rain Water 收集雨水

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

  5. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  6. LeetCode - 42. Trapping Rain Water

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

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

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

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

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

  9. 【LeetCode】42. Trapping Rain Water

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

随机推荐

  1. [转] gdb中忽略信号处理

    信号(Signals) 信号是一种软中断,是一种处理异步事件的方法.一般来说,操作系统都支持许多信号.尤其是UNIX,比较重要应用程序一般都会处理信号.UNIX定义了许 多信号,比如SIGINT表示中 ...

  2. LSI SAS 2208 配置操作

    配置LSISAS2208 介绍LSISAS2208扣卡的配置方法. 2.1 登录CU界面 介绍登录LSISAS2208的CU配置界面的方法,以及CU界面的主要功能. 2.2 创建RAID 介绍创建RA ...

  3. linux下查看文件系统类型

    1. df -hT命令   -h, --human-readable  print sizes in human readable format (e.g., 1K 234M 2G) -T, --pr ...

  4. 【iOS开发】emoji表情的输入思路

    1.自定义一个表情包类继承NSTextAttachment #import <UIKit/UIKit.h> /** 表情包的自定义类*/ @interface EmojiTextAttac ...

  5. svg text文字居中

    <text x="100" y="100" text-anchor="middle" dominant-baseline=" ...

  6. Linux学习——环境变量设置

    一般来说,配置交叉编译工具链的时候需要指定编译工具的路径,此时就需要设置环境变量.例如我的mips-linux-gcc编译器在“/opt/au1200_rm/build_tools/bin”目录下,b ...

  7. PHP HTTP

    安装 HTTP 函数是 PHP 核心的组成部分.无需安装即可使用这些函数. PHP HTTP 函数 PHP:指示支持该函数的最早的 PHP 版本. 函数 描述 PHP header() 向客户端发送原 ...

  8. XMLHttpRequest state以及readystate的对应值

    status状态值长整形标准http状态码,定义如下: Number  Description  100 Continue101 Switching PRotocols200 OK201 Create ...

  9. rsync指令详解

    rsync指令详解(更详细的看官方文档http://rsync.samba.org/ftp/rsync/rsync.html) [root@Centos epel]# rsync --help rsy ...

  10. Gradle一分钟实现Spring-MVC

    前提: 1,已安装JDK 2, 有Intellij IDEA 3, 已安装Gradle 一分钟实现步骤: 1,mkdir Spring-MVC;cd Spring-MVC2,gradle init3, ...