Solved LeetCode program · 42 of 1,000
#0042 · Trapping Rain Water
Original English source
The heights is a list of 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, and 1. The water is 0.
For every position from 0 to the last position of heights.
The current is item position of heights. The lefthighest is 0. For every earlier from 0 to position, if item earlier of heights is greater than lefthighest, set lefthighest to item earlier of heights. The righthighest is 0. For every later from position to the last position of heights, if item later of heights is greater than righthighest, set righthighest to item later of heights. The boundary is the smaller of lefthighest and righthighest. The depth is boundary minus current. If depth is more than 0, add depth to water.
Show water.
Expected stdout
6