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. J2EE开发中常用的缓存策略

    一.什么是缓存1.Cache是高速缓冲存储器 一种特殊的存储器子系统,其中复制了频繁使用的数据以利于快速访问2.凡是位于速度相差较大的两种硬件/软件之间的,用于协调两者数据传输速度差异的结构,均可称之 ...

  2. 这是一个在Windows live 上实验的文章

    这是一个windows 实验用的文章,希望一次成功

  3. LA 3485 Bridge

    自适应辛普森公式模板. #include<algorithm> #include<iostream> #include<cstring> #include<c ...

  4. C primer plus 读书笔记第八章

    本章的标题是字符输入/输出和输入确认.主要内容是讨论用于I/O的标准函数. 1.getchar()和putchar() 这两个函数之前用过,我们通过这两个函数来讨论下缓冲区. #include &qu ...

  5. Union和Union All的差别

    如果我们有一个表Student,包含下面字段与数据: drop table student; create table student ( id int primary key, name nvarc ...

  6. Cocos2D-X2.2.3学习笔记12(瞬间动作)

    到眼下我们已经学习了有 坐标系统 内存管理 UI系统 事件处理 几何图形 今天我们来学习动作管理OK 我们来看看类结构图 CCAction   全部动作的基类 以下派生了三个子类:CCFiniteTi ...

  7. 函数学习(JY07-JavaScript-JS基础03)

  8. mongodb查询之模糊查询

    mongodb的模糊查询是用正则表达式来实现的.例子如下: db.COMMODITY_COMMODITY_SHOP.find({name:{$regex :/南京/i}})

  9. phpmyadmin登陆提示#2002 无法登录 MySQL 服务器和设置自增

    看看mysql启动没有,结果是mysql服务没有启动,找了半天,是这个原因,那就右键计算机->管理->服务->启动mysql服务 设置自增:在显示出来的一行字段定义中把浏览器的滚动条 ...

  10. 杂记之web篇

    问题1:通过POST方式提交给后台的数据出现了乱码,用部分浏览器测试却是好的. 解决办法: 在web.config文件中加上 <globalization responseEncoding=&q ...