LeetCode42.TrappingRainWater-O(n)可以在五秒内理解的思路墙(top)的位置(top_index)。然后,对于墙的左侧部分,right_max已经确定;对于墙的右侧,已确定left_max(均等于顶部)。之后分别计算左右两侧的蓄水量,期间分别保持left_max和right_max。欢迎在我的博客上轻松探索更多想法代码类解决方案:deftrap(self,height:List[int])->int:result=0iflen(height)<3:return0top=max(height)top_index=height.index(max(height))max_left=height[0]max_right=height[-1]iftop_index>=1:foriinrange(1,top_index,1):result+=max(0,min(max_left,top)-height[i])max_left=max(max_left,height[i])iftop_index
