Topic:
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.
Exercises
Iterate through the array and find the local minimum value, either if the current value is greater than or equal to the previous value, or if the current value is greater than the last value. After finding the local minimum value, then left to the left to find the maximum value, and then to the right to the maximum value, to find the right maximum value when it is larger than the left to stop looking. Then figure out the amount of water that can be loaded from the left maximum to the right maximum, and then continue to find the local minimum from the position of the right maximum, and so on until the complete array is traversed. (From here)
Solution 1 ()
classSolution { Public: intTrap (vector<int>&height) { intresult =0, n = height.size (), left =1, right =0; for(intI=1; i<n-1; i++) { if(Height[i] >= height[i-1] || Height[i] > height[i+1])Continue; for(left = i1; Left>0; --Left ) { if(height[left]>=height[left-1]) Break; } Right= i +1; for(intj=i+1; j<n; ++j) {if(height[j]>=Height[right]) { Right=J; if(Height[right]>=height[left]) Break; } } inth =min (height[left],height[right]); for(intk=left+1; k<right; ++k) {if(H>height[k]) result + = h-Height[k]; } I=Right ; } returnresult; }};
For each value, the largest amount of rainwater collected by a container with two other values is certainly obtained with the maximum value of the left and right sides as the container's two walls, specifically in the title of the difference between the smaller and the current values of the two largest values (if the minimum value is greater than the current value, no rainwater is collected). Initializes a one-dimensional array of vector<int> DP (n,0) using dynamic planning. Then traverse the array two times, the first traversal from the left scan to find the current position to the left of the maximum value, and stored in the DP, the second traversal from the right side of the scan to find the current position to the right of the maximum value, and then the DP in the left side of the maximum value, save the lower of the two values, and compare this If it is greater than the current value, the total amount of rainwater collected should be added to the difference between this small value and the current value. Use an array of left[i] to indicate the height of the highest pillar on the left of the Pillar I (from here)
Use an array of right[i] to indicate the height of the highest pillar to the right of the pillar I
1. Scan from left to right, left[i] = max (left[i-1], a[i-1])
2. Right-to-left scan, right[i] = max (right[i + 1], A[i + 1])
3. The water on the pillar I can save is min (Left[i], right[i])-a[i], because at this time left[i] is useless, it can be used to store this value. (From here)
Complexity: Time O (n), space O (n)
Solution 2 ()
classSolution { Public: intTrap (vector<int>&height) { intresult =0, Lmax =0, Rmax =0, n =height.size (); Vector<int> DP (N,0); for(intI=0; i<n; i++) {Dp[i]=Lmax; Lmax=Max (Lmax, Height[i]); } for(inti=n-1; i>=0; i--) {Dp[i]=min (dp[i], rmax); Rmax=Max (Rmax, Height[i]); if(Dp[i] >Height[i]) {Result+ = Dp[i]-Height[i]; } } returnresult; }};
According to the above analysis of rainwater harvesting, the rainwater collection of any 3 values is the difference between the lower and middle values of the two sides (if the former is greater than the latter). Therefore, you can set two pointers to traverse from both ends to the middle, which is the value of the two walls of the container. Low walls (smaller values) determine the amount of rainwater collected, so the median value is set to a subsequent value from a smaller value, and the collected rainwater is calculated until a value greater than this small value is encountered and the smaller value is updated to the current value. This will only take a single scan to solve the problem. (From here)
Solution 3 ()
classSolution { Public: intTrap (vector<int>&height) { intresult =0, left =0, right = Height.size ()-1; while(Left <Right ) { inth =min (Height[left], height[right]); if(H = =Height[left]) { ++Left ; while(Left < right && Height[left] <h) {result+ = h-height[left++]; } } Else { --Right ; while(Left < right && Height[right] <h) {result+ = h-height[right--]; } } } returnresult; }};
The idea is:
1) find the highest bar.
2) traverse the bar from the left of the highest bar.becasue we have the highest bar in right, so, any bar higher than it right Bar (s) can contain the water.
3) traverse the bar from right the highest bar.becasue we had the highest bar in left, so, any bar higher than it left B AR (s) can contain the water. (From here Chenhao)
Solution 4 ()
classSolution { Public: intTrapintA[],intN) {intresult =0; //find the highest value/position intMaxhigh =0; intMaxidx =0; for(intI=0; i<n; i++){ if(A[i] >Maxhigh) {Maxhigh=A[i]; Maxidx=i; } } //From the left to the highest postion intPrevhigh =0; for(intI=0; i<maxidx; i++){ if(A[i] >Prevhigh) {Prevhigh=A[i]; } result+ = (Prevhigh-A[i]); } //From the right to the highest postionPrevhigh=0; for(inti=n-1; i>maxidx; i--){ if(A[i] >Prevhigh) {Prevhigh=A[i]; } result+ = (Prevhigh-A[i]); } returnresult; }};
"Leetcode" 042 trapping Rain water