We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4be887d commit 241af0aCopy full SHA for 241af0a
leetcode/42.trapping-rain-water.py
@@ -32,7 +32,31 @@
32
33
34
class Solution:
35
+ # two pointer solution
36
+ # https://leetcode.com/problems/trapping-rain-water/solution/
37
def trap(self, height: List[int]) -> int:
38
+ length = len(height)
39
+ # forward
40
+ lmax = rmax = 0
41
+ lp = 0
42
+ rp = length - 1
43
+ result = 0
44
+ while lp < rp:
45
+ if height[lp] < height[rp]:
46
+ if height[lp] > lmax:
47
+ lmax = height[lp]
48
+ else:
49
+ result += lmax - height[lp]
50
+ lp += 1
51
52
+ if height[rp] > rmax:
53
+ rmax = height[rp]
54
55
+ result += rmax - height[rp]
56
+ rp -= 1
57
+ return result
58
+
59
+ def trap2(self, height: List[int]) -> int:
60
length = len(height)
61
# forward
62
last = 0
0 commit comments