-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trapped_water.py
30 lines (24 loc) · 1 KB
/
Trapped_water.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def trap(height):
"""
:type height: List[int]
:rtype: int
"""
# Find maxLeftHeight and maxRightHeight for all the elements in the array and store their values in another array
# waterTrapped += min(maxLeft, maxRight) - height[i]
leftMax=[]
leftMax.append(0) # leftMax height of 1st element is zero
result = 0
# print("Height: " + str(height))
for i in range(1, len(height)):
leftMax.append(height[i-1] if height[i-1]>leftMax[i-1] else leftMax[i-1])
# print("leftMax: " + str(leftMax))
rightMax=[0]*len(height)
for i in range(len(height)-2, -1, -1):
rightMax[i] = height[i+1] if height[i+1]>rightMax[i+1] else rightMax[i+1]
# rightMax.append(0)
# print("rightMax: " + str(rightMax))
for i in range(len(height)):
result += min(leftMax[i], rightMax[i]) - height[i] if (min(leftMax[i], rightMax[i]) - height[i])>0 else 0
return result
height = [0,1,0,2,1,0,1,3,2,1,2,1]
print(trap(height))