【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/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的 ...
随机推荐
- pandas error记录随笔
1.sys:1: DtypeWarning: Columns (0,1) have mixed types. Specify dtype option on import or 解决办法:PANDAS ...
- 开发Maven插件
Mojo: Maven plain Old Java Object 1.插件命名规则:maven-<yourplugin>-plugin是Maven的保留字段,不允许使用,我们可以用< ...
- C# linq根据自定义筛选条件和所对应的数值进行筛选
在软件应用中有时候会出现这样的界面:上面是利用多选框和下拉框组合的筛选条件.下面表格展示筛选后的数据.如下图 上面是筛选条件,表格是根据筛选条件筛选的结果. 如果表格不支持筛选功能.可以利用Linq对 ...
- 美国L1签证面谈的时候一般VO会问到什么问题?
L签证:L签证签发给被其中国公司调派到美国分公司或合资公司工作的人员.申请人必须将在美国担任经理级职务或具有专业知识,且在申请签证前的三年中至少为同一雇主或公司连续工作至少一年.签证签发费将因签证的入 ...
- jQuery_1_基础核心
jQuery代码风格:在jQuery程序中,不管是页面元素的选择还是内置的功能函数,都是以“$"来起始的. $(function(){}); / ...
- SAP云平台运行环境Cloud Foundry和Neo的区别
SAP云平台提供了两套运行环境:Cloud Foundry和Neo 从下图能发现,Cloud Foundry的运行环境,基础设施由第三方公司提供,比如Amazon亚马逊和Microsoft微软,SAP ...
- Producer & Consumer
需要与Eureka结合使用 Eureka环境搭建 Producer 一.pom文件 <?xml version="1.0" encoding="UTF-8" ...
- IOS 线程描述
●什么是线程 ● 1个进程要想执行任务,必须得有线程(每1个进程至少要有1条线程) ● 线程是进程的基本执行单元,一个进程(程序)的所有任务都在线程中执行 ● 比如使用酷狗播放音乐.使用迅雷下载电影, ...
- python_26_dictionary
#key-value 字典无下标 所以乱序,key值尽量不要取中文 info={ 'stu1101':'Liu Guannan', 'stu1102':'Wang Ruipu', 'stu1103': ...
- 【转】转自微信公众号 JavaScript 复杂判断的更优雅写法
与微信公众号看到一篇js复杂判断的文章,对我启发很大,故转到博客园以供后期不断学习并应用于项目.原文地址:https://mp.weixin.qq.com/s/ClFDRj4MnAxv1dJ5VWKS ...