-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrappingRainWater.py
More file actions
57 lines (49 loc) · 2.48 KB
/
TrappingRainWater.py
File metadata and controls
57 lines (49 loc) · 2.48 KB
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
class Solution:
def trap(self, height: List[int]) -> int:
if len(height) <= 2:
return 0
water_content = 0
start_point = 0
end_point = len(height) - 1
while start_point < end_point:
if height[start_point] <= height[end_point]:
current_height = height[start_point]
while start_point < end_point and current_height >= height[start_point]:
water_content += current_height - height[start_point]
start_point += 1
else:
current_height = height[end_point]
while start_point < end_point and current_height >= height[end_point]:
water_content += current_height - height[end_point]
end_point -= 1
# print("start point is", start_point)
# print("end point is", end_point)
# sub_water_content = 0
# while end_point < len(height):
# if end_point == len(height) - 1:
# break
# while end_point < len(height) - 1 and height[end_point + 1] <= height[end_point]:
# end_point += 1
# # print(end_point)
# while end_point < len(height) - 1 and height[end_point + 1] >= height[end_point]:
# end_point += 1
# # print(end_point)
# # pdb.
# sub_water_content_start = start_point + 1
# if height[start_point] >= height[end_point]:
# while sub_water_content_start < end_point:
# diff = height[end_point] - height[sub_water_content_start]
# if diff > 0:
# water_content += diff
# sub_water_content_start += 1
# else:
# while sub_water_content_start < end_point:
# diff = height[start_point] - height[sub_water_content_start]
# if diff > 0:
# water_content += diff
# sub_water_content_start += 1
# # print("start point is", start_point)
# # print("end point is", end_point)
# # print("water content is", water_content)
# start_point = end_point
return water_content