-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42.py
More file actions
29 lines (25 loc) · 839 Bytes
/
42.py
File metadata and controls
29 lines (25 loc) · 839 Bytes
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
class Solution:
def trap(self, height: List[int]) -> int:
lMax = height[0]
rMax = height[-1]
i = 0
j = len(height) - 1
updatedI = True
waterTotal = 0
while (i < j):
#print("lMax: " + str(lMax) + ", rMax: " + str(rMax))
if (updatedI):
#print("i: " + str(i))
waterTotal += max(0, min(lMax, rMax) - height[i])
else:
#print("j: " + str(j))
waterTotal += max(0, min(lMax, rMax) - height[j])
if (lMax <= rMax):
i += 1
lMax = max(height[i], lMax)
updatedI = True
else:
j -= 1
rMax = max(height[j], rMax)
updatedI = False
return waterTotal