forked from algorhythms/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
041 Trapping Rain Water.py
48 lines (35 loc) · 1.35 KB
/
041 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
"""
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it
is able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
"""
__author__ = 'Danyang'
class Solution:
def trap(self, A):
"""
Simplified version of Palantir Technology Online Test 2013
Algorithm: forward scanning & backward scanning, pointers algorithm
O(n)
dp, store the max
:param A: a list of integers
:return: an integer
"""
left_maxs = [0 for _ in A] # on the left including itself
right_maxs = [0 for _ in A] # on the right including itself
left_max = 0
for ind, val in enumerate(A):
left_max = max(left_max, val)
left_maxs[ind] = left_max
right_max = 0
# for ind, val in enumerate(reversed(A)): # ind incorrect
for ind, val in reversed(list(enumerate(A))):
right_max = max(right_max, val)
right_maxs[ind] = right_max
# calculate the volume
volume = 0
for ind, val in enumerate(A):
volume += max(0, min(left_maxs[ind], right_maxs[ind]) - val)
return volume
if __name__=="__main__":
print Solution().trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1])