[LeetCode][Java] 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.
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
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!
题目:
给定n个非负整数来代表高度图。每一个栏的宽度为1.计算下雨之后这个东东能最多能盛多少的水。
比方,给定[0,1,0,2,1,0,1,3,2,1,2,1],
返回 6.
上图的高度图通过数组[0,1,0,2,1,0,1,3,2,1,2,1]表示出来。
这个样例中雨水(蓝色所看到的)共6个单位。
算法分析:
当刷到这个题的时候我真是醉了。这就是15年春季的阿里算法project师实习在线笔试的题目~~
一模一样,当时水笔的我真心不会做啊,笔试果断没过 囧~~
* 观察下就能够发现被水填满后的形状是先升后降的塔形。因此。先遍历一遍找到塔顶,然后分别从两边開始,往塔顶所在位置遍历,水位仅仅会增高不会减小,
* 且一直和近期遇到的最大高度持平,这样知道了实时水位。就能够边遍历边计算面积。
* 首先找到最高的,然后从左往最高处扫。
* 碰到一个数A[i]。计算A[0,,,i-1]最高的是否高过A[i]。
* 假设是。则A[i]上的水的体积为max(A[0...i-1])-A[i],否则为0而且更新最大值
AC代码:
public class Solution
{
public int trap(int[] height)
{
if(height==null||height.length==0)
return 0;
int res=0;
int maxvalue=0;
int label=0;
int startmvalue=0;
int endmvalue=0;
int mtem;
for(int i=0;i<height.length;i++)
{
if(height[i]>maxvalue)
{
maxvalue=height[i];
label=i;
}
}
startmvalue=height[0];
for(int i=0;i<label;i++)
{
if(height[i]>startmvalue) startmvalue=height[i];
else
{
res+=startmvalue-height[i];
}
}
endmvalue=height[height.length-1];
for(int i=height.length-1;i>label;i--)
{
if(height[i]>endmvalue) endmvalue=height[i];
else
{
res+=endmvalue-height[i];
}
}
return res;
}
}
[LeetCode][Java] Trapping Rain Water的更多相关文章
- [LeetCode] 407. Trapping Rain Water II 收集雨水 II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] 407. 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 - 42. Trapping Rain Water
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- LeetCode 042 Trapping Rain Water
题目要求:Trapping Rain Water Given n non-negative integers representing an elevation map where the width ...
随机推荐
- PHP开发心得四
1,php返回给html页面的Json数据不能含有回车符 某次用php编写查询数据库数据,以json格式返回给前端页面js文件,js文件以angularJS的函数调用处理的方式进行数据显示,但数据返回 ...
- Farseer.net轻量级开源框架 入门篇:Where条件的终极使用
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 查询数据详解 下一篇:Farseer.net轻量级开源框架 中级篇: 事务的使用 ...
- 【译】x86程序员手册30-8.2 I/O指令
8.2 I/O Instructions I/O指令 The I/O instructions of the 80386 provide access to the processor's I/O p ...
- Oracl常用e函数整理
最近学Oracle数据库,常常遇到Oracle数据库函数问题,经过默默地琢磨处理,总结了一些Oracle数据库常用函数. ------------------------------------ -- ...
- HDU_1556_线段树区间更新
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Android五大布局介绍&属性设置大全
前言 在进行Android开发中,常常需要用到各种布局来进行UI的绘制,今天我们就来讲下Android开发中最常用的五大布局介绍和相关属性的设置. 目录 Android五大布局介绍&属性设置. ...
- Discuz!伪静态原理分析
伪静态在seo火热的时代,是每个站长都比较关注的问题,discuz!论坛如何伪静态,为什么伪静态失效了,为什么列表页无法实现伪静态,为什么有些页面不是伪静态呢?下面dz官方nxy105从两个角度入手为 ...
- [转]Js获取当前日期时间及其它操作
转载自:http://www.cnblogs.com/carekee/articles/1678041.html Js获取当前日期时间及其它操作 var myDate = new Date();myD ...
- 03C++基本数据类型
基本数据类型 2.2.1整型数据 短整型(short int) 有符号短整型(signed short int) 无符号短整型(unsigned short int) 一般整型(int) 有符号一般整 ...
- 13jsp、javaWeb开发模式
13jsp.javaWeb开发模式-2018/07/25 1.jsp jsp实际上就是servlet.jsp=html+java,为用户提供动态内容 不适合编写Java逻辑 2.JSP原理 翻译(生成 ...