class Solution(object): def trap(self, height): """ :type height: List[int] :rtype: int """ sum = 0 for i in range(1, len(height) - 1): # search tallest wall in left left_h = max(height[:i])
# search tallest wall in right right_h = max(height[i+1:])
min_tmp = min(left_h, right_h) if height[i] < min_tmp: sum += (min_tmp - height[i])
class Solution(object): def trap(self, height): """ :type height: List[int] :rtype: int """ # search tallest wall in left left_max = [0 for i in range(len(height))] for i in range(1, len(height) - 1): left_max[i] = max(left_max[i-1], height[i-1])
# search tallest wall in right right_max = [0 for i in range(len(height))] for i in range(len(height) - 2, 0, -1): right_max[i] = max(right_max[i+1], height[i+1])
sum = 0 for i in range(1, len(height) - 1): min_tmp = min(left_max[i], right_max[i]) if min_tmp > height[i]: sum += (min_tmp - height[i]) return sum