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:/ ...
随机推荐
- java导出简单写法
List<SaleOrderExportDTO> list = TransferUtil.transferList(queryList, SaleOrderExportDTO.class) ...
- linux ftp 上传与下载命令解析
month=`date -d "last month" +"%Y%m"` year=`date +"%Y"` rm /home/yourDi ...
- 初识Flutter
什么是Flutter 官网的定义如下: Flutter is a new project to help developers build high-performance, high-fidelit ...
- 与Mysqli相关的四种数据库取值
<!--取值方案一:通过数字数组 fetch_row()--><meta http-equiv="Content-Type" content="text ...
- FineReport---过滤条件
1.过滤条件,获取值 该单元格的值 ,是当team为A,content为产量 数据列过滤条件常用处理:nofilter.left.and ,or 2.单元格值等查询条件区域的内容 所选你应该指的就是日 ...
- 《挑战程序设计竞赛》2.6 数学问题-快速幂运算 POJ1995
POJ3641 此题应归类为素数. POJ1995 http://poj.org/problem?id=1995 题意 求(A1^B1+A2^B2+ - +AH^BH)mod M. 思路 标准快速幂运 ...
- 让WebApi支持Namespace
环境:VS2012 .net 4.0 参考: http://aspnet.codeplex.com/SourceControl/changeset/view/dd207952fa86#Samples/ ...
- 四.mysql演示银行转账
代码演示: #conding:utf8 import pymysql import sys class TransferMoney(object): def __init__(self,conn): ...
- 携程greenlet模块使用
import greenlet def f1(): print(11) gr2.switch() print(22) gr2.switch() def f2(): print(33) gr1.swit ...
- 我的Android进阶之旅------>关于使用Android Studio替换App的launcher图标之后仍然显示默认的ic_launcher图标的解决方法
前言 最近做了一个App,之前开发该App的时候一直以来都是默认的launcher图标启动的, 今天美工换了一个App的launcher 图标,因此在Android Studio中将默认的lanche ...