Given n non-negative integers representing an elevation map where the width of each bar are 1, compute how much WA ter It is the 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 the Rain Water (blue section) is being trapped. Thanks Marcos for contributing this image!
classSolution { Public: //clamping force guidelines. Let the low side move every time. Until we met . intTrap (vector<int>&height) { intlow=0, High=height.size ()-1, res=0; intleftmax=0, rightmax=0; while(low<=High ) { if(height[low]<Height[high]) {Leftmax=Max (Leftmax,height[low]); Res+=leftmax-Height[low]; Low++; } Else{Rightmax=Max (Rightmax,height[high]); Res+=rightmax-Height[high]; High--; } } returnRes; }};
Trapping Rain Water