Java [Leetcode 42]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.

解题思路:
思路一:开辟两个数组空间,逐个遍历数组,找出该位置左边的最大值与右边的最大值,分别放到两个数组中。然后对整个数组进行遍历,位置装水后的值不能超过该位置左右最高值中的最小数。该算法需三次遍历数组,但是时间复杂度为O(n);空间需开辟两个数组空间,空间复杂度为O(n)。
代码如下:
public class Solution {
public int trap(int[] height) {
int length;
int maxLeftHeight = 0, maxRightHeight = 0;
int result = 0;
int temp;
if (height == null || (length = height.length) == 0)
return 0;
int[] leftMaxHeight = new int[length];
int[] rightMaxHeight = new int[length];
for (int i = 0; i < length; i++) {
leftMaxHeight[i] = maxLeftHeight;
maxLeftHeight = Math.max(maxLeftHeight, height[i]);
}
for (int i = length - 1; i >= 0; i--) {
rightMaxHeight[i] = maxRightHeight;
maxRightHeight = Math.max(maxRightHeight, height[i]);
}
for (int i = 0; i < length; i++) {
temp = Math.min(leftMaxHeight[i], rightMaxHeight[i]);
if (temp >= height[i])
result += temp - height[i];
}
return result;
}
}
思路二:
设置两个指示变量,分别存放当前指向的两个位置。找出左位置的左边的最高值和右位置的右边的最高值。对于两者中的最小值,表明当前位置加上水过后的值不超出该值,那么相减即可,反之,对另一个相减。该算法只需要一次遍历数组,所以效率更高,时间复杂度为O(n);空间方面不需要开辟数组空间,所以为常数空间。
代码如下:
public class Solution {
public int trap(int[] height) {
int length;
int left, right;
int maxLeftHeight = 0, maxRightHeight = 0;// 记录当前位置左边, 右边的最大值
int result = 0;
if (height == null || (length = height.length) == 0)
return 0;
left = 0;
right = length - 1;
while (left < right) {
maxLeftHeight = Math.max(maxLeftHeight, height[left]);
maxRightHeight = Math.max(maxRightHeight, height[right]);
if (maxLeftHeight < maxRightHeight) {
result += maxLeftHeight - height[left];
left++;
} else {
result += maxRightHeight - height[right];
right--;
}
}
return result;
}
}
Java [Leetcode 42]Trapping Rain Water的更多相关文章
- 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
42. Trapping Rain Water Problem's Link ------------------------------------------------------------- ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- [LeetCode] 42. Trapping Rain Water 解题思路
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [leetcode]42. Trapping Rain Water雨水积水问题
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- LeetCode 42 Trapping Rain Water(积水体积)
题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description Problem: 根据所给数组的值,按照上图的示意 ...
随机推荐
- xaml中绑定单例属性
在项目中经常会遇到,同一个字典表绑定到多个ItemsControl上的情况,可以在单例中创建一个List,xaml上绑定即可.看代码: 1,XAML <Grid> <StackPan ...
- oracle 表空间、用户名 相关语句
一.oracle查询表空间文件所在路径 select * from dba_data_files t where t.tablespace_name='FLW' 二.计算出表空间各相关数据 SELE ...
- c# 模拟表单提交,post form 上传文件、大数据内容
表单提交协议规定:要先将 HTTP 要求的 Content-Type 设为 multipart/form-data,而且要设定一个 boundary 参数,这个参数是由应用程序自行产生,它会用来识别每 ...
- ASIHTTPRequest的使用(转)
转载自:http://fushengfei.iteye.com/blog/1147112 博客分类: IOS 原文地址:http://wiki.magiche.net/pages/viewpage ...
- 微软职位内部推荐-SENIOR PRODUCER
微软近期Open的职位: Role Based in Shanghai, ChinaTitle: ProducerWe are seeking a Senior Producer to lead Pr ...
- Ubuntu 设置root用户登录
由于 Ubuntu 是基于 Debian 的 linux 操作系统,在默认的情况下,是没有超级用户(superuser, root)的,但有些系统操作必须有超级用户的权限才能进行,如手动释放内存等. ...
- 实时数据处理环境搭建flume+kafka+storm:1.zookeeper 安装配置
1. 解压 tar -zxvf 2.创建目录 zk根目录创建 mkdir zkdatalog --日志 mkdir zkdata ---快照文件 3.修改配置文 ...
- 各系统下设置输入法按键为ctrl+shift+space
xp=====(一直找不到..原来右边是可以下拉的).. linux ibus----- 设置---直接按下Ctrl+Shift+Space
- 1509 -- Glass Beads POJ
题意:求一个字符串的最小表示的开始下标 就当模板题写了 把字符串重复一遍,再建后缀自动机,贪心的选最小字典序在上面走len步 因为走出来的一定是子串,长度又是len,所以一定是原来的字符串旋转得到的, ...
- 怎样快糙猛的开始搞Kaggle比赛