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!
Idea 1:
This problem can be scanned by layer, each time the lowest non-0 height is lost. But the problem is that if the height is greater than 0 and the number of distinct is M, loop m. It's easy to time out.
The specific code is as follows:
Public intTrap (int[] height) { intres =0; intCount =0; foreach(varHinchHeight) count+=h; while(count>1) { intj =0; while(Height[j] = =0) {J++; } intTrap =J; intA =0; intMintrap =Height[j]; for(intI =j+1;i< height. Count (); i++) { if(Height[i]! =0) {Mintrap=math.min (Mintrap,height[i]); A+ = Math.max (0, i-trap-1); Trap=i; }} Res+ = A *Mintrap; for(inti =0;i< height. Count (); i++) { if(height[i]>0) {Count-=Mintrap; Height[i]-=Mintrap; } } } returnRes; }
Solution 2:
The position of each slot, the amount of water it can store is the difference between its maximum height on the left and the highest height on the right and its own height, for example, if at i=5 position, I left highest position is 9, the right highest position is 7, then the amount of water stored in this place is Math.min (9,7)-5 = 2. So we set up a DP array, dp[i] record I position to the left of the highest height, left to traverse again, set a DP array, dp[i] record I position to the right of the highest height, the right to traverse again, and then loop again to calculate the size.
Code is as follows:
Public intTrap (int[] height) { intres =0; intSize =height. Count (); varLeftmax =New int[size]; varRightmax =New int[size]; for(inti =1;i< size;i++) {Leftmax[i]= Math.max (height[i-1],leftmax[i-1]); } for(intI =size-2; i>=0; i--) {Rightmax[i]= Math.max (height[i+1], rightmax[i+1]); } for(inti =1;i< size-1; i++) { intMin =math.min (Leftmax[i],rightmax[i]); if(height[i]<min) {Res+ = min-Height[i]; } } returnRes; }
Solution 3:
We can also use a stack, which is slower, and stack is pressed into a stack. Peek () Small value, so that every 0 will be pressed in. Calculate when Height[i] is greater than stack. Peek time, you need to pop a value out, this is the bottom, and then use the stack. The minimum value of Peek () and Height[i], minus the bottom, then multiplied by the difference of index. This place because
Public intTrap (int[] height) { intMax =0; intres =0; intSize =height. Count (); if(Size <=2)returnRes; inti =0; varstack =Newstack<int> ();//Store the Largerst heigth index while(i<size) { if(Stack. Count () = =0|| Height[i] <= height[stack. Peek ()]) stack. Push (i++); Else { intbot =stack. Pop (); Res+ = (stack. Count () = =0)?0:((Math.min (height[stack. Peek ()], height[i])-Height[bot]) * (I-stack. Peek ()-1));; } } returnRes; }
Trapping Rain Water