[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
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!
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; } }
相关文章推荐
- Android Studio开发JNI
- AndroidStudio生成全局变量的快捷键
- android 开源组件
- JS判断客户端是否是iOS或者Android
- Android中Parcelable接口用法
- android.app.FragmentManager 与 android.support.v4.app.FragmentManager带来的若干Error
- android service是否在同一个内存的问题
- Android 实现对话框圆角功能
- Unity3d热更新全书-加载(一)从AssetBundle说起
- Android安全输入设计与思考
- Android自定义横向的ProgressBar
- Android中Js与java的交互
- Android对象序列化存储
- Android JNI 获取应用程序签名
- Android APP内存优化之图片优化 ,不错的文章
- iOS中多线程原理与runloop介绍
- Android_selector、shape
- Android学习系列(40)--Android主题和样式之系统篇
- Unity3D学习(视频教程)
- Android Support ;v4、v7、v13的区别