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!
class Solution { public: int trap(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if(n<3) return 0; int start,end,sum; start=0,end=0,sum=0; for( ; end<n ; ){ if( A[start]<A[end] && end>start ) start=end; else{ sum+=A[end]-A[start]; end++; } } return sum; } };
上面这个代码是不行的,因为没有考虑到A[end]可能不会再比start大了,所以我就分开来,再递归调用一次
class Solution { public: int trap(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if(n<3) return 0; int start,end,sum,sumt; start=end=sum=sumt=0; for( ; end<n ; ){ if(A[start]<=A[end] && end>start){ sum+=sumt; sumt=0; start=end; } else{ sumt+=A[start]-A[end]; end++; if(end==n && start!=n){ int count=end-start; int *p=new int[count]; for(int i=0 ; i<count ; i++){ p[i]=A[--end]; } sumt=trap(p,count); return sumt+sum; } } } return sum; } };
class Solution { public: int trap(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<int> left(n); int maxHeight = 0; for(int i = 0; i < n; i++) { left[i] = maxHeight; maxHeight = max(maxHeight, A[i]); } vector<int> right(n); maxHeight = 0; for(int i = n - 1; i >= 0; i--) { right[i] = maxHeight; maxHeight = max(maxHeight, A[i]); } int water = 0; for(int i = 0; i < n; i++) { int height = min(left[i], right[i]) - A[i]; if (height < 0) height = 0; water += height; } return water; } };