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.
2 parents d12bd37 + 377d0f8 commit e143827Copy full SHA for e143827
python/0042-trapping-rain-water.py
@@ -1,18 +1,16 @@
1
class Solution:
2
def trap(self, height: List[int]) -> int:
3
- if not height:
4
- return 0
5
6
- l, r = 0, len(height) - 1
7
- leftMax, rightMax = height[l], height[r]
8
- res = 0
9
- while l < r:
10
- if leftMax < rightMax:
11
- l += 1
12
- leftMax = max(leftMax, height[l])
13
- res += leftMax - height[l]
14
- else:
15
- r -= 1
16
- rightMax = max(rightMax, height[r])
17
- res += rightMax - height[r]
18
- return res
+ c = height.index(max(height))
+
+ vol = 0
+ for arr in [height[:c], height[:c:-1]]:
+ first = 0
+ for i in arr:
+ if i < first:
+ vol += first - i
+ else:
+ first = i
+ return vol
0 commit comments