-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrapping_rain_water.py
55 lines (40 loc) · 1.29 KB
/
trapping_rain_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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from typing import List
class Solution:
def trap_old(self, height: List[int]) -> int:
length = len(height)
if not height or length <= 2:
return 0
l_max = [0 for _ in height]
l_max[0] = height[0]
r_max = [0 for _ in height]
r_max[length - 1] = height[length - 1]
for i in range(1, length):
l_max[i] = max(l_max[i - 1], height[i])
for i in range(length - 2, -1, -1):
r_max[i] = max(r_max[i + 1], height[i])
total = 0
for i in range(length):
height_delta = min(l_max[i], r_max[i])
curr = height[i]
if height_delta - curr > 0:
total += height_delta - curr
return total
def trap(self, heights):
N = len(heights)
l, r = 0, N - 1
lmax, rmax = 0
total = 0
while l < r:
if heights[l] < heights[r]:
if heights[l] >= lmax:
lmax = heights[l]
else:
total += lmax - heights[l]
l += 1
else:
if heights[r] >= rmax:
rmax = heights[r]
else:
total += rmax - heights[r]
r -= 1
return total