LeetCode_Trapping Rain Water
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(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n <= ) return ;
vector<int> left(n,);
vector<int> right(n, );
left[] = A[];
for(int i =; i < n;++i)
{
left[i] = left[i-] > A[i] ? left[i-] : A[i];
}
right[n-] = A[n-];
for(int i = n-; i>= ;--i){
right[i] = right[i+] > A[i] ? right[i+] : A[i];
}
int res = ;
for(int i = ; i< n; i++){
int min = left[i] < right[i]? left[i] : right[i];
res += min - A[i];
}
return res;
}
};
LeetCode_Trapping Rain Water的更多相关文章
- [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 Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- 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 ...
随机推荐
- App项目升级Xcode7&iOS9(续) - This bundle is invalid. The bundle identifier contains disallowed characters
金田 iOS 9发布已经有2月有余,现在Xcode已经有升级到Xcode7.1,开发环境安装等一系列相关的流程,以及Xcode 7 & iOS 9升级相关的一些部分,在这里就不再多加赘述(详见 ...
- M0、M1、M2、M3都是用来反映货币供应量的重要指标
m2-反映货币供应量的重要指标编辑词条m2广义货币是一个经济学概念,和狭义货币相对应,货币供给的一种形式或口径,以M2来表示,其计算方法是交易货币(M1,即社会流通货币总量加上活期存款)以及定期存款与 ...
- MD中bitmap源代码分析--数据结构
本篇分析bitmap的数据结构的设计,并基于此分析bitmap的工作机制. 为了后面更清楚的理解,先有个总体印象,给出整体的结构图: 在下面的描述中涉及到的内容可以对照到上图中相应部分,便于理解. 首 ...
- memory_target not supported on this system
- wpf下拉框不能多选的原因
<dxe:ComboBoxEdit Margin="0" Height="25" Width="65" VerticalAlignm ...
- EBuild-API常见问题汇总
问题1:用户访问被防火墙屏蔽检查处理流程 E-Build API(原IBE)是面向航空公司和代理人,以及第三方的航空预订服务产品.用户通过客户端,使用固定IP访问E-Build API服务器,航信网络 ...
- java反射--注解的定义与运用以及权限拦截
自定义注解类编写的一些规则: 1. Annotation型定义为@interface, 所有的Annotation会自动继承java.lang.Annotation这一接口,并且不能再去继承别的类或是 ...
- Android(java)学习笔记260:JNI之native方法头文件的生成
1. JDK1.6 ,进入到工程的bin目录下classes目录下: 使用命令: javah packageName.ClassName 会在当前目录下生成头文件,从头文件找到jni协议方法 下面举 ...
- LinkButton( 按钮)
一. 加载方式 //class 加载方式<a href="###" class="easyui-linkbutton">按钮</a> / ...
- Python代码分析工具之dis模块
转自:http://hi.baidu.com/tinyweb/item/923d012e8146d00872863ec0 ,格式调整过. 代码分析不是一个新的话题,代码分析重要性的判断比较主观,不同 ...