码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode], solution, non-code implementation

时间:2020-01-23 09:32:11      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:any   The   new   lock   some   span   trap   i+1   man   

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

标签:any   The   new   lock   some   span   trap   i+1   man   

原文地址:https://www.cnblogs.com/sarah-zhang/p/12230152.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!