leetcode数组之Trapping Rain Water
来源:互联网 发布:网络教育专科2月毕业 编辑:程序博客网 时间:2024/06/08 05:23
描述
Givennnon-negativeintegersrepresentinganelevationmapwherethewidthofeachbaris1,compute
how much water it is able to trap aer raining.
For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
题意:
给定n个非负整数,代表一个柱状图,每一个柱子的宽度为1,计算下雨之后柱状图能装多少水?
例如:
[0,1,0,2,1,0,1,3,2,1,2,1] 返回 6
//方法一:对于每个柱子,找到其左右两边最高的柱子,该柱子能容纳的面积就是 min(leftMostHeight,rightMostHeight) - height。所以,//1. 从左往右扫描一遍,对于每个柱子,求取左边最大值;//2. 从右往左扫描一遍,对于每个柱子,求最大右值;//3. 再扫描一遍,把每个柱子的面积并累加。//时间复杂度 O(n),空间复杂度 O(n) //代码一:int trap(int A[],int len){ if(A==NULL||len<=0) return 0; int *leftMostHeight=new int[len](); int *rightMostHeight=new int[len](); int maxHeight=0; //找到数组中每个数其左边最大值,存入数组leftMostHeight; for(int i=0;i<len;i++) { leftMostHeight[i]=maxHeight; if(A[i]>maxHeight) maxHeight=A[i]; } maxHeight=0; //找到数组中每个数其右边最大值,存入数组rightMostHeight; for(int j=len-1;j>=0;j--) { rightMostHeight[j]=maxHeight; if(A[j]>maxHeight) maxHeight=A[j]; } int water=0; for(int k=0;k<len;k++) { int curWater=min(leftMostHeight[k],rightMostHeight[k])-A[k]; if(curWater>0) water+=curWater; } delete[]leftMostHeight; delete[]rightMostHeight; return water; }//代码二:int trap(vector<int> &height){ const int len=height.size(); if(len<=0) return 0; int leftMostHeight[len]; int rightMostHeight[len]; leftMostHeight[0]=0; rightMostHeight[len-1]=0; for(int i=1;i<len;i++) { leftMostHeight[i]=max(leftMostHeight[i-1],height[i-1]); rightMostHeight[len-1-i]=max(rightMostHeight[len-i],height[len-i]); } int water=0; for(int i=0;i<len-1;i++) { int curWater=min(leftMostHeight[i],rightMostHeight[i])-height[i]; if(curWater>0) water+=curWater; } return water;} //方法二://1. 扫描一遍,找到最高的柱子,这个柱子将数组分为两半;//2. 处理左边一半;//3. 处理右边一半。//时间复杂度 O(n),空间复杂度 O(1)class Solution { public: int trap(vector<int> &height){ int len=height.size(); if(len<=0) return 0; int max=0; for(int i=0;i<len;i++) { if(height[i]>height[max]) max=i; } int water=0; for(int i=0,peak=0;i<max;i++) { if(height[i]>peak) peak=height[i]; else water+=peak-height[i]; } for(int i=len-1,peak=0;i>max;i--) { if(height[i]>peak) peak=height[i]; else water+=peak-height[i]; } return water;}};
0 0
- leetcode数组之Trapping Rain Water
- leetcode之Trapping Rain Water
- leetcode之Trapping Rain Water
- LeetCode之Trapping Rain Water
- Leetcode之Trapping Rain Water
- 【Leetcode】之Trapping Rain Water
- [leetcode] 【数组】42. Trapping Rain Water
- LeetCode: Trapping Rain Water
- LeetCode : Trapping Rain Water
- [LeetCode] Trapping Rain Water
- [Leetcode] Trapping Rain Water
- [Leetcode] Trapping Rain Water
- [LeetCode]Trapping Rain Water
- LeetCode-Trapping Rain Water
- [leetcode] Trapping Rain Water
- [LeetCode] Trapping rain water
- Leetcode: Trapping Rain Water
- Leetcode:Trapping Rain Water
- 安卓线程切换优雅的写法
- Android Studio签名打包的两种方式
- 018.py
- 019.py
- Intiall Eclipse PyDev&…
- leetcode数组之Trapping Rain Water
- Apache win10的安装
- sysbench压力测试工具使用详细说明
- 020.py
- 一个小成就~~~Machine learning 也…
- A simple network …
- ibatis占位符$与#区别
- 17.4.17 漫画与人脸识别(一)
- 《将博客搬至CSDN》