-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode_Trapping_Rain_Water.py
59 lines (46 loc) · 1.52 KB
/
Leetcode_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
56
57
58
59
### Solution One ### Space complexity O(1) ###
class Solution(object):
def trap(self, height):
"""
:type height: List[int]
:rtype: int
"""
left, right = 0, len(height) - 1
result = 0
leftMax, rightMax = height[left], height[right]
while left < right:
if leftMax <= rightMax:
left += 1
leftMax = max(leftMax, height[left])
result += leftMax - height[left]
else:
right -= 1
rightMax = max(rightMax, height[right])
result += rightMax - height[right]
return result
### Solution Two ### Space complexity O(n) ###
class Solution(object):
def trap(self, height):
"""
:type height: List[int]
:rtype: int
"""
new_list_Left = []
new_list_Right = []
min_list = []
result = 0
max_so_far_left = height[0]
max_so_far_right = height[len(height) - 1]
for value in height:
new_list_Left.append(max_so_far_left)
max_so_far_left = max(max_so_far_left, value)
for value in height[::-1]:
new_list_Right.append(max_so_far_right)
max_so_far_right = max(max_so_far_right, value)
new_list_Right.reverse()
for i in range(len(height)):
min_list.append(min(new_list_Left[i], new_list_Right[i]))
new_list = [max(min_list[i] - height[i], 0) for i in range(len(height))]
for i in range(len(height)):
result += new_list[i]
return result