【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/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的 ...
随机推荐
- 构建第一个Spring Boot2.0应用之集成dubbo上---环境搭建(九)
一.环境: Windows: IDE:IntelliJ IDEA 2017.1.1 JDK:1.8.0_161 Maven:3.3.9 springboot:2.0.2.RELEASE Linux(C ...
- JAXB介绍二
链接上一遍 JAXB介绍一 , 本节主要介绍解析xml的步骤, 下面的例子是在实际项目中运用的, 把它拿出来单独写一个java运行程序. 5. 测试实例 先给出我的代码结构图: 再给出要解析的Scri ...
- C#启动或停止 计算机中“服务”
第一.要添加一个引用System.ServiceProcess 第二.要在程序中使用命名空间ServiceProcess 代码片段: using System.ServiceProcess; Serv ...
- python3基础05(有关日期的使用1)
#!/usr/bin/env python# -*- coding:utf-8 -*- import timefrom datetime import datetime,timedelta,timez ...
- C#之linq
本文根据30分钟LINQ教程学习作的笔记. 1.Guid.Empty Guid 结构: 表示全局唯一标识符 (GUID).Empty字段:Guid 结构的只读实例,其值均为零.用来设置初始值. G ...
- HDU1195 双向BFS(或BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1195 , 双向BFS或者直接BFS也可以过. 其实这道题只是单向BFS就可以过的,但是为了练算法,所以 ...
- Dynamic typing 动态类型
https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Dyna ...
- Solaris&&QNX® Neutrino®&&OpenVMS&&FreeBSD&&AIX
原文链接Solaris (读作 /se'laris:/ 或者 /so'le:ris/ 或者 '梭拉瑞斯' )是Sun Microsystems研发的计算机 操作系统.它被认为是UNIX操作系统的衍生版 ...
- 干净的架构The Clean Architecture_软件架构系列
本文转载自:https://www.jdon.com/artichect/the-clean-architecture.html ,这个博客站很有历史了,博主经常翻译Github大牛的文章,值得墙裂推 ...
- GNU C中__attribute__
__attribute__基本介绍: 1. __attribute__ 可以设置函数属性.变量属性和类型属性. 2. __attribute__ 语法格式为:__attribute__ ((attri ...