42. Trapping Rain Water
we need to find how many waters can each block[i] trap. So we need to find the left peak from block_0 to block[i-1] and find the right peak from block[i+1] to block[n-1]. See the following pseudo-code:
total_water = 0;
for i = 0 to n-1:
Lmax = Max(h[0], h[1], ... h[i-1]);
Rmax = Max(h[i+1], h[i+2], ... h[n-1]);
water_and_building = Min(Lmax, Rmax);
wi = water_and_building - h[i]; // the building has some height
total_water += wi;
return total_water;
[LeetCode], solution, non-code implementation
原文:https://www.cnblogs.com/sarah-zhang/p/12230152.html