forked from lupamo3-zz/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Water_Area.py
43 lines (37 loc) · 1.23 KB
/
Water_Area.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
# Time O(n) | Space O(n)
def waterArea(heights):
maxes = [0 for x in heights]
leftMax = 0
for i in range(len(heights)):
height = heights[i]
maxes[i] = leftMax
leftMax = max(leftMax, height)
rightMax = 0
for i in reversed(range(len(heights))):
height = heights[i]
minHeight = min(rightMax, maxes[i])
if height < minHeight:
maxes[i] = minHeight - height
else:
maxes[i] = 0
rightMax = max(rightMax, height)
return sum(maxes)
def waterArea(heights):
leftMaxHeights = [0 for _ in heights]
currentLeftMax = 0
for i in range(len(heights)):
height = heights[i]
leftMaxHeights[i] = currentLeftMax
currentLeftMax = max(currentLeftMax, height)
rightMaxHeights = [0 for _ in heights]
currentRightMax = 0
for i in reversed(range(len(heights))):
height = heights[i]
rightMaxHeights[i] = currentRightMax
currentRightMax = max(currentRightMax, height)
maxes = [0 for _ in heights]
for i in range(len(heights)):
minHeight = min(leftMaxHeights[i], rightMaxHeights[i])
if minHeight > heights[i]:
maxes[i] = minHeight - heights[i]
return sum(maxes)