Test Instructions:
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!
Title:
A given n non-negative integer to represent the height graph. The width of each column is 1. Calculate the amount of water that can be filled by this thing after the rain.
[0,1,0,2,1,0,1,3,2,1,2,1" 6
The height graph is represented by an array [0,1,0,2,1,0,1,3,2,1,2,1].
In this example, the rain (seen in blue) has a total of 6 units.
Algorithm Analysis:
I was really drunk when I brushed this question. This is the 15 spring of the ALI algorithm project teacher Internship Online test Questions ~ ~
Exactly, when the pen I really do not do ah, written a decisive not embarrassed ~ ~
* Observation can be found after the water is filled with the shape of the first ascending and descending tower shape. So. First go through to find the tower, and then start from both sides, the top of the tower to traverse the location, the water level will only increase not decrease,
* and has been the most recently encountered the highest level of parity, so know the real-time water level. You can calculate the area while traversing the edge.
* First to find the highest, then sweep from left to highest point.
* hit a number a[i]. Calculates whether the highest a[0,,, I-1] is higher than a[i].
* Suppose it is. The volume of water on A[i] is Max (a[0...i-1])-a[i], otherwise 0 and the maximum value is updated
AC Code:
public class Solution {public int trap (int[] height) { if (height==null| | height.length==0) return 0; int res=0; int maxvalue=0; int label=0; int startmvalue=0; int endmvalue=0; int mtem; for (int i=0;i
[Leetcode][java] trapping Rain water