[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 ...
随机推荐
- linux ssh 利用scp传输文件
使用方式如下: 1.上传本地文件到服务器 scp /path/filename username@servername:/path/ 例如scp /var/www/test.php root@192. ...
- vue组件---动态组件&异步组件
(1)在动态组件上使用keep-alive 之前曾经在一个多标签的界面中使用 is 特性来切换不同的组件.接下来简单回顾下 <component>元素是vue 里面的一个内置组件.在里面使 ...
- Microsoft SQL Server 存储过程
Microsoft SQL Server 存储过程 TRIGGER DDL触发器:主要用于防止对数据库架构.视图.表.存储过程等进行的某些修改:DDL事件是指对数据库CREATE,ALTER,DROP ...
- 15Oracle Database 索引
Oracle Database 索引 索引 索引的目的是加快查询速度,就像一本数据的目录一样.建立索引的原则:非常少的DML操作:经常出现在where语句中的字段 2.20.1.建立索引 l 对t_ ...
- Unity中播放带有alpha通道格式为Mp4的视频
问题: Unity中实现播放透明的MP4视频时出现黑点 解决办法: 使用Unity自带的shader去除黑点 1:shader代码如下所示 Shader "Unlit/NewUnlit ...
- 牛客多校Round 9
Solved:1 rank:112 E. Music Game 题解说有个非简化的原题 bzoj4318 #include <bits/stdc++.h> using namespace ...
- 在添加新内容时,creatat没有数据
首先找到controller中的add方法,然后是执行了Service.insert()方法 然后找到service对应的impl方法, impl方法中的 public void insert(Cus ...
- java8 foreach不能使用break、countinue
在学习1.8新特性的过程中,发现foreach中不可以使用break和countinue,然后我使用了return,结果如下图,对循环并没有影响. 百度一下,发现了一个不一样的回答 然后我就看了下源码 ...
- geth搭建以太坊私链及常用操作
一.下载安装geth客户端 https://www.ethereum.org/ 二.搭建私有链 1.准备创世区块配置文件 要运行私有链,我们就需要定义自己的创世区块,创世区块信息写在一个json格式的 ...
- Session共享实现方案调研
1.背景 随 着互联网的日益壮大,网站的pv和uv成线性或者指数倍的增加.单服务器单数据库早已经不能满足实际需求.目前大多数大型网站的服务器都采用了分布式服务 集群的部署方式,所谓集群,就是让一组计算 ...