Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

trapping rain water

# Python program to find maximum amount of water that can 
# be trapped within given set of bars. 

def findWater(arr, n): 
	# left[i] contains height of tallest bar to the 
	# left of i'th bar including itself 
	left = [0]*n 

	# Right [i] contains height of tallest bar to 
	# the right of ith bar including itself 
	right = [0]*n 

	# Initialize result 
	water = 0
   
	# Fill left array 
	left[0] = arr[0]
	print(left[-1])
	for i in range( 1, n): 
		left[i] = max(left[i-1], arr[i])
		print(f"left[i]=",left[i])

	# Fill right array 
	right[n-1] = arr[n-1] 
	
	print(f"n",n)
	for i in range(n-2, -1, -1): 
		right[i] = max(right[i+1], arr[i])
		print(f"right[i+1]",right[i+1])
	print(f"right=",right)
	print(f"left=",left)
	print(f"arr=",arr)
	# Calculate the accumulated water element by element 
	# consider the amount of water on i'th bar, the 
	# amount of water accumulated on this particular 
	# bar will be equal to min(left[i], right[i]) - arr[i] . 
	for i in range(0, n): 
		water += min(left[i],right[i]) - arr[i]
		print(f"water",water)

	return water 


# Driver program 

arr = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1] 
n = len(arr) 
print("Maximum water that can be accumulated is",findWater(arr, n)) 

# This code is contributed by 
# Smitha Dinesh Semwal 

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.