首页 > 其他 > 详细

[LeetCode], solution, non-code implementation

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!