-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.py
85 lines (73 loc) · 2.5 KB
/
answer.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python3
#------------------------------------------------------------------------------
# First Solution
#------------------------------------------------------------------------------
class Solution:
def trap(self, height):
"""
:type height: List[int]
:rtype: int
"""
water = 0
water_differences1 = [0]*len(height)
trapped1 = []
curr = 0
beg = 0
# Go left to right
for i in range(len(height)):
if height[i] >= curr:
curr = height[i]
if i - beg > 1:
trapped1.append((beg, i))
beg = i
else:
water_differences1[i] = (curr - height[i])
curr = 0
water_differences2 = [0]*len(height)
trapped2 = []
end = len(height)-1
# Go right to left
for i in range(len(height)-1, -1, -1):
if height[i] >= curr:
curr = height[i]
if end - i > 1:
trapped2.append((i, end))
end = i
else:
water_differences2[i] = (curr - height[i])
# Combine lists and get rid of dupes
trapped = list(set(trapped1).union(set(trapped2)))
# Go through the guarenteed trapped water list and get the right amount
for i, j in trapped:
for k in range(i+1, j):
water += min(water_differences1[k], water_differences2[k])
return water
#------------------------------------------------------------------------------
# Second Solution
#------------------------------------------------------------------------------
class Solution:
def trap(self, height):
"""
:type height: List[int]
:rtype: int
"""
r, redge = len(height) - 1, len(height) - 1
l, ledge = 0, 0
ans = 0
# Go from left and right until they meet
while l < r:
if height[l] <= height[r]:
l += 1
if height[l] < height[ledge]:
ans += height[ledge] - height[l]
else:
ledge = l
else:
r -= 1
if height[r] < height[redge]:
ans += height[redge] - height[r]
else:
redge = r
return ans
#------------------------------------------------------------------------------
#Testing