forked from netsetos/python_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrain_trapped.py
More file actions
34 lines (25 loc) · 818 Bytes
/
rain_trapped.py
File metadata and controls
34 lines (25 loc) · 818 Bytes
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
def rain_trap(arr):
size=len(arr)
water =0
left =size *[0]
right = size * [0]
left[0]=arr[0]
max_so_far_left=arr[0]
for index in range(0, size):
if(max_so_far_left < arr[index]):
max_so_far_left=arr[index]
left[index]=max_so_far_left
else:
left[index] = max_so_far_left
max_so_far_right = arr[-1]
for index in range(size-1, -1, -1):
if(max_so_far_right < arr[index]):
max_so_far_right = arr[index]
right[index]=max_so_far_right
else:
right[index] = max_so_far_right
for index in range(0,size):
water = water + min(left[index],right[index])-arr[index]
return water
A=[ 1, 0, 2, 0, 1, 0, 3, 1, 0, 2 ]
print(rain_trap(A))