Skip to content

Commit 3ab07d1

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent b0f4a25 commit 3ab07d1

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

dynamic_programming/trapped_water.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,25 @@ def trapped_rainwater(heights: tuple[int, ...]) -> int:
3434
return 0
3535
if any(h < 0 for h in heights):
3636
raise ValueError("No height can be negative")
37-
left,right=0,len(heights)-1
38-
leftmax,rightmax=0,0
39-
water=0
40-
while left <right:
37+
left, right = 0, len(heights) - 1
38+
leftmax, rightmax = 0, 0
39+
water = 0
40+
while left < right:
4141
if heights[left] < heights[right]:
4242
if heights[left] >= leftmax:
43-
leftmax=heights[left]
43+
leftmax = heights[left]
4444
else:
45-
water+=leftmax - heights[left]
46-
left+=1
45+
water += leftmax - heights[left]
46+
left += 1
4747
else:
4848
if heights[right] >= rightmax:
49-
rightmax=heights[right]
49+
rightmax = heights[right]
5050
else:
51-
water+=rightmax - heights[right]
52-
right-=1
51+
water += rightmax - heights[right]
52+
right -= 1
5353
return water
5454

55+
5556
if __name__ == "__main__":
5657
import doctest
5758

0 commit comments

Comments
 (0)