【Trapping Rain Water】cpp
题目:
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(vector<int>& height) {
// non valid input
int len = height.size();
if (len<=) return ;
// initial
int left_max[height.size()];
int right_max[height.size()];
left_max[] = ;
right_max[len-] = ;
// get left_max and right_max
for (int i = ; i < len; ++i)
{
left_max[i] = std::max(left_max[i-], height[i-]);
right_max[len-i-] = std::max(right_max[len-i], height[len-i]);
}
// calculate the sum
int sum = ;
for (int i = ; i < len; ++i)
{
int h = std::min(left_max[i], right_max[i]);
if (h>height[i])
{
sum += h-height[i];
}
}
return sum;
}
};
Tips:
1. 遍历,获得每个位置上左边最高的和右边最高的;选择左边和右边比较小的高度,减去该位置的高度,就是可需水量。
2. 注意一些极端case的处理
=================================================
第二次过这道题,思路没有完全记清,稍微捡了一下思路,一次AC。
class Solution {
public:
int trap(vector<int>& height) {
if ( height.size()< ) return ;
// left height
vector<int> l(height.size(),);
int l_heighest = ;
for ( int i=; i<height.size(); ++i )
{
l_heighest = std::max(l_heighest, height[i]);
l[i] = l_heighest;
}
// right height
vector<int> r(height.size(),);
int r_heighest = ;
for ( int i=height.size()-; i>=; --i )
{
r_heighest = std::max(r_heighest, height[i]);
r[i] = r_heighest;
}
// total trapping water
int ret = ;
for ( int i=; i<height.size(); ++i )
{
int h = std::min(l[i], r[i]);
ret += h - height[i];
}
return ret;
}
};
tips:
这道题就是一句话口诀:左右短的,减去当前位置高度,等于当前位置可需水量。
【Trapping Rain Water】cpp的更多相关文章
- leetcode 【 Trapping Rain Water 】python 实现
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- 【LeetCode】42. Trapping Rain Water
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- [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 ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LintCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- 有意思的数学题:Trapping Rain Water
LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的 ...
随机推荐
- #include< >和#include“ ”的区别
< >引用的是编译器的类库路径里面的头文件 " "引用的是你程序目录的相对路径中的头文件 假如你编译器定义的自带头文件引用在C:\Keil\c51\INC\下面 则#i ...
- ArcGIS API for JavaScript开发初探——基础知识
1.前言 在ArcGIS Web API开发体系中一共有四大类,分别为: ArcGIS API for Flex ArcGIS API for JavaScript ArcGIS API for RE ...
- sublime完美编码主题
Theme – Soda 使用Ctrl+Shift+P快捷键或者进入菜单:Preferences(首选项) - Package Control(插件控制),调出命令输入框,输入Install Pack ...
- 基于mllib的协同过滤实战(电影推荐)
//加载需要的包 import org.apache.spark.rdd._ import org.apache.spark.mllib.recommendation.{ALS, Rating, Ma ...
- “ipconfig不是内部命令或外部命令”解决方法
第一:用鼠标右键单击“计算机”,在弹出的下拉菜单中选择“属性”. 第二:在系统属性中选择“高级系统设置”.在系统属性对话框中找到其上方的“高级”选项卡,里面有一个“环境变量”按钮,点击进入 第三:在下 ...
- Copy Failed Error Access to fobidden
使用SVN的branch/tag命令来切包时,报出了一下错误提示 就这个错误,调了整整一天,如果你也遇到了类似问题,我真心希望下面解决问题的过程能帮助到你:D 解决过程: 1.网上查了很多帖子,发现跟 ...
- Mybatis-延迟加载和缓存
延迟加载 概念: 就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据.延迟加载也称懒加载. 好处:先从单表查询,需要时再从关联表去关联查询,大大提高数据库性能,因为查询单表要比关联查询多张表 ...
- Java 发送邮件工具类
1. Mail.java package util; import java.util.Date; import java.util.Properties; import javax.mail.Au ...
- 更换PostgreSql的data文件夹并重新服务器(此方法同样适用于系统崩溃后,找回数据的操作)
*如果是系统崩溃,需要找回数据,PostgreSQL安装目录的data文件夹要存在 1.备份PostgreSQL安装目录到其他目录下 2.停止Postgres服务,可以在运行中输入services.m ...
- bootstrapValidator 插件
1 有关内容:https://blog.csdn.net/u013938465/article/details/53507109 https://blog.csdn.net/wangtongxue12 ...