您的位置:首页 > 移动开发

[leedcode 42] Trapping Rain Water

2015-07-10 18:20 483 查看
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
.



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!

public class Solution {
//动态规划,构造两个数组leftMax和rightMax,分别代表左侧最高的柱子和右侧最高的柱子
//leftMax[i]=Math.max(leftMax[i-1],height[i-1],右侧亦然,注意leftMax[i]表示i左侧最大的柱子,不包括i
//因此每个节点能够装水的容量可以通过Math.min(rightMax[i],leftMax[i])-height[i],求装水的总容量时,只需遍历该数组即可。
//注意求每个节点装水的容量,可以复用之前的rightMax数组。
//此解法时间复杂度是O(n),空间复杂度也是O(n)
public int trap(int[] height) {
/* int res=0;
int len=height.length;
if(len<=2) return res;
int leftMax[]=new int[len];
leftMax[0]=0;
int rightMax[]=new int[len];
rightMax[len-1]=0;
for(int i=1;i<len;i++){
leftMax[i]=Math.max(leftMax[i-1],height[i-1]);
}
for(int i=len-2;i>=0;i--){
rightMax[i]=Math.max(rightMax[i+1],height[i+1]);
}
for(int i=0;i<len;i++){
rightMax[i]=Math.min(rightMax[i],leftMax[i])-height[i];
if(rightMax[i]>0) res+=rightMax[i];
}
return res;*/

//此解法的时间复杂度是O(n),只需要遍历一遍。空间复杂度是O(1)
//思路是采用l和r两个指针,维护装水两边的位置。
//当l处高度低时,说明l右侧装的水肯定和l处一样高,此时逐步右移l,同是加上l处与右移后位置高度差(因为这里都能装水啊),直到再遇到同样         //高或者更高的位置。然后进行下一轮判断。
//同样,当r处高度低时,说明r左侧的水肯定和r处一样高,此时逐步左移r,同是加上r处与左移后位置高度差,直到再遇到同样高或者更高的位置。
//最后直到l和r相遇,结束。
int res=0;
int l=0;
int len=height.length;
int r=len-1;
while(l<r){
int temp=Math.min(height[l],height[r]);
if(temp==height[l]){
l++;
while(l<r&&height[l]<=temp){
res+=temp-height[l];
l++;
}

}
else{
r--;
while(l<r&&height[r]<=temp){
res+=temp-height[r];
r--;
}

}
}
return res;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: