【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/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的 ...
随机推荐
- Java集合框架—Map
Map集合:该集合存储键值对.一对一对往里存.而且要保证键的唯一性. 1,添加. put(K key, V value) putAll(Map<? extends K,? extends V& ...
- jQuery-名称符号$与其他库函数冲突
1.通过全名替代简写的方式来使用 jQuery jQuery("button").click(function(){ jQuery("p").text(&quo ...
- Python变量状态保持四种方法
Python状态保持 全局 global def tester(start): global state state = start def nested(label): global state ...
- IPC Gateway 设计
1. IPC Gateway对外提供的功能: IPC的register/request/reply/notification服务. 2. IPC Gatew的实现原理: 各个具体的服务注册自己的回调函 ...
- 2018.2.5 PHP如何写好一个程序用框架
随着PHP标准和Composer包管理工具的面世,普通开发者撸一个框架已经不再是什么难事了. 无论是路由管理.ORM管理.还是视图渲染都有许许多多优秀的包可以使用.我们就像堆积木一样把这些包用comp ...
- 启动tomcat的Cannot find ./catalina.sh 的问题
从终端进入tomcat的bin目录,然后执行startup.sh Cannot find bin/catalina.sh The file is absent or does not have exe ...
- centos 7 虚拟机启用网卡
1.vi /etc/sysconfig/network-scripts/ifcfg-enp0s3 2.编辑默认网卡配置文件,将ONBOOT由no改为yes,编辑完成后,按ESC回至命令模板,输入&qu ...
- 如何让图片相对于上层DIV始终保持水平、垂直都居中
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- kernel
http://sebastianraschka.com/Articles/2014_kernel_pca.html
- Bootstrap历练实例:按钮(Button)插件单个切换
单个切换 如需激活单个按钮的切换(即改变按钮的正常状态为按压状态,反之亦然),只需向 button 元素添加 data-toggle="button" 作为其属性即可,如下面实例所 ...