TrappingRainWater
问题描述:
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.

算法分析:观察下就可以发现被水填满后的形状是先升后降的塔形,因此,先遍历一遍找到塔顶,然后分别从两边开始,往塔顶所在位置遍历,水位只会增高不会减小,且一直和最近遇到的最大高度持平,这样知道了实时水位,就可以边遍历边计算面积。
public class TrapingRainWater
{
public int trap(int[] height)
{
int n = height.length;
if(n <= 2)
{
return 0;
}
int max = -1;
int maxIndex = 0;
for(int i = 0; i < n; i ++)
{
if(height[i] > max)
{
max = height[i];
maxIndex = i;
}
}
int area = 0;
int root = height[0];
for(int i = 0; i < maxIndex; i ++)
{
if(root < height[i])
{
root = height[i];
}
else
{
area += (root - height[i]);
}
}
root = height[n-1];
for(int i = n-1; i > maxIndex; i --)
{
if(root < height[i])
{
root = height[i];
}
else
{
area += (root - height[i]);
}
}
return area;
} }
TrappingRainWater的更多相关文章
- leetcode — trapping-rain-water
/** * Source : https://oj.leetcode.com/problems/trapping-rain-water/ * * Created by lverpeng on 2017 ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- leetcode算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- BUG-FREE-For Dream
一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...
- 转载 ACM训练计划
leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode. ...
- LeetCode题目分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- [LeetCode]题解(python):042-Trapping Rain Water
题目来源 https://leetcode.com/problems/trapping-rain-water/ Given n non-negative integers representing a ...
- Trapping Rain Water [LeetCode]
Problem Description: http://oj.leetcode.com/problems/trapping-rain-water/ Basic idea: Get the index ...
- <转>LeetCode 题目总结/分类
原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...
随机推荐
- Null value was assigned to a property of primitive type setter
org.springframework.orm.jpa.JpaSystemException: Null value was assigned to a property of primitive t ...
- 巨蟒python全栈开发-第10天 函数进阶
一.今日主要内容总览(重点) 1.动态传参(重点) *,** *: 形参:聚合 位置参数*=>元组 关键字**=>字典 实参:打散 列表,字符串,元组=>* 字典=>** 形参 ...
- ffmpeg命令
1 将mp4格式的视频文件转换成mkv格式 ffmpeg -i input.mp4 -vcodec copy -acodec copy output.mkv
- 小团队交流为什么 :wq! :wq 二者结果一致?
w 答案: :q 执行失败--->提示-已经修改,但是尚未保存,+!强制不保存退出 :w 保存
- flask实现获取表单并执行shell
1.一个HTML form input和一个button提供给用户输入 2.使用flask的request获取用户输入的文件名 3.判断输入异常 4.执行shell命令touch aa.txt 并返回 ...
- centos上源码安装clang 3.8
之前想在centos系统上安装clang 3.6版本,由于yum上版本太低,想通过源码编译安装.按照网上说的源码安装步骤,下好llvm.clang.clang-tools-extra和compiler ...
- Delphi 正则表达式之TPerlRegEx 类的属性与方法(6): EscapeRegExChars 函数
Delphi 正则表达式之TPerlRegEx 类的属性与方法(6): EscapeRegExChars 函数 // EscapeRegExChars 函数可以自动为特殊字符加转义符号 \ var ...
- Codeforces Round #396 (Div. 2) A - Mahmoud and Longest Uncommon Subsequence B - Mahmoud and a Triangle
地址:http://codeforces.com/contest/766/problem/A A题: A. Mahmoud and Longest Uncommon Subsequence time ...
- CodeForces - 919D Substring (拓扑排序+dp)
题意:将一个字符串上的n个字符视作点,给出m条有向边,求图中路径上最长出现的相同字母数. 分析:首先如果这张图中有环,则可以取无限大的字符数,在求拓扑排序的同时可以确定是否存在环. 之后在拓扑排序的结 ...
- sql developer 如何格式化sql
1.首先 Ctrl+A 全选需要格式的sql 2.然后 Ctrl+F7 即可格式化