[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 ...
随机推荐
- Android Studio 入门 Hello World
Android Studio 入门 Hello World Gavin要加油 1.5k 6月22日 发布 推荐 1 推荐 收藏 17 收藏,2.1k 浏览 引言 前两天开始学习android开发,本来 ...
- SQL关于触发器及存储过程的创建
使用T-SQL语句来创建触发器 基本语句如下﹕ create trigger trigger_name on {table_name | view_name} {for | After | Ins ...
- axios方法get及post代码示例
show: function(){ //get方式 //赋值给变量self var self = this; var url = "hotcity.json"; axios.get ...
- ThinkPHP---案例2--职员管理功能
[一]准备工作 (1)创建菜单,修改跳转路径 <li> <a href="javascript:;" class="workerManage" ...
- Unity中播放带有alpha通道格式为Mp4的视频
问题: Unity中实现播放透明的MP4视频时出现黑点 解决办法: 使用Unity自带的shader去除黑点 1:shader代码如下所示 Shader "Unlit/NewUnlit ...
- block: cfq 学习01
CFQ,即Completely Fair Queueing绝对公平调度器,力图为竞争块设备使用权的所有进程分配一个等同的时间片,在调度器分配给进程的时间片内,进程可以将其读写请求发送给底层块设备,当进 ...
- xmpp之配置Xcode(1)
介绍 ios上的XMPPFramework你能够在Xcode/iPhoneXMPP 目录找到,它只是实现了XMPP的一小部分功能. 下面主要介绍在开发XMPPFramework ios应用之前的配置工 ...
- 洛谷——P2007 魔方
P2007 魔方 常神牛家的魔方都是3*3*3的三阶魔方,大家都见过. 模拟即可: #include<iostream> #include<cstdio> #include&l ...
- buf.writeInt32BE()函数详解
buf.writeInt32BE(value, offset[, noAssert]) buf.writeInt32LE(value, offset[, noAssert]) value {Numbe ...
- Placing Lampposts
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=91212#problem/E #include <iostream> #inc ...