human
Trapping Rain Water | Function to return the maximum water that can be stored.
Stores the indices of the bars.
size of the array.
Stores the final result.
Loop through the each bar.
Remove bars from the stack until the condition holds.
store the height of the top and pop it ..
If the stack does not have any bars or the the popped bar has no left boundary.
Get the distance between the left and right boundary of popped bar.
Calculate the min . height.
If the stack is either empty or height of the current bar is less than or equal to the top bar of stack.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to trapping Rain Water and the other steps required.
 def maxWater ( height ) :
	stack = [ ]
	n = len ( height )
	ans = 0
	for i in range ( n ) :
		while ( len ( stack ) != 0 and ( height [ stack [ - 1 ] ] < height [ i ] ) ) :
			pop_height = height [ stack [ - 1 ] ]
			stack . pop ( )
			if ( len ( stack ) == 0 ) :
				break
			distance = i - stack [ - 1 ] - 1
			min_height = min ( height [ stack [ - 1 ] ] , height [ i ] ) - pop_height
			ans += distance * min_height
		stack . append ( i )
	return ans
arr = [ 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ]
print ( maxWater ( arr ) )