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. 利用powershell进行远程服务器管理(命令行模式)

    Pssession,Pssession是Windows Powershell会话的意思,一个会话,可以共享数据,提供交互式的对话,我们可以为某些命令例如Invoke-Command 制定会话来远程作业 ...

  2. c语言学习之第四章

    第四章 第四章主要介绍了分支结构,循环结构的简单使用,还有其他简单的语句结束语句,比如,break,continue.还有gote语句.下面是我学习C语言第四章的一些心得和总结. 1简单的if语句 简 ...

  3. paip.gch预编译头不生效的原因以及解决:

    paip.gch预编译头不生效的原因以及解决: 作者Attilax ,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/a ...

  4. machine learning in action , part 1

    We should think in below four questions: the decription of machine learning key tasks in machine lea ...

  5. activiti总结2

    根据流程号查询失败原因. activiti重试机制.齿轮节点.邮件节点.任务节点.ACT_HI_ACTINST历史表.ACT_RU_EXECUTION运行表.看图. 在Eclipse里面自己写个测试方 ...

  6. 让sublime支持gbk常用编码

    Sublime Text 2是一个非常不错的源代码及文本编辑器,但是不支持GB2312和GBK编码在很多情况下会非常麻烦.不过Sublime Package Control所以供的插件可以让Subli ...

  7. 在vim中设置 '打印时间'的快捷键.

    在 ~/.vimrc (没有该文件可以手动创建)中输入 map <F4> <Esc>:r !date<CR> 实现在 '一般模式'状态点击 F4时,自动在vim中打 ...

  8. 初级班 Linux使用

    ifconfig 查看IP地址 重新启动网卡服务 service network restart 笔记 1.通过远程工具登陆到linux后,所在的位置是当前登录用户的家目录(home director ...

  9. php中双$$与多$$

    <?php$a="b";$b="bbb";$c="ccc";echo $$a;?> 输出结果bbb $a的值为b $$a不是输出 ...

  10. intellij idea 热部署失效,需要手动编译类

    从网上看到的解决方案,做一下备忘: spring boot项目中遇到jrebel类需要手动编译才会触发热部署的问题(spring boot devtools一样的问题) 1.ctl + shift + ...