42. Trapping Rain Water
Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6解题要点:
class Solution(object):
def trap(self, height):
"""
:type height: List[int]
:rtype: int
"""
if height == None or len(height) == 0:
return 0
leftDP = [0] * len(height)
rightDP = [0] * len(height)
leftDP[0] = height[0]
rightDP[-1] = height[-1]
for i in range(1, len(height)):
leftDP[i] = max(leftDP[i-1], height[i])
for j in range(len(height)-2, -1, -1):
rightDP[j] = max(rightDP[j+1], height[j])
res = 0
for k in range(len(height)):
res += min(leftDP[k], rightDP[k]) - height[k]
return resLast updated
