0% found this document useful (0 votes)
128 views24 pages

Presentation Trapping Water

This document discusses different algorithms to calculate the maximum amount of rainwater that can be trapped between bars of varying heights. It begins by providing an example and describing the brute force method that has O(n^2) time complexity. It then explains the dynamic programming approach that uses two auxiliary arrays to store maximum bar heights from the left and right, improving the time complexity to O(n). Finally, it proposes a two pointer method that calculates trapped water using left and right max heights without auxiliary arrays, having O(n) time and O(1) space complexity. Pseudocode and a Python implementation are provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views24 pages

Presentation Trapping Water

This document discusses different algorithms to calculate the maximum amount of rainwater that can be trapped between bars of varying heights. It begins by providing an example and describing the brute force method that has O(n^2) time complexity. It then explains the dynamic programming approach that uses two auxiliary arrays to store maximum bar heights from the left and right, improving the time complexity to O(n). Finally, it proposes a two pointer method that calculates trapped water using left and right max heights without auxiliary arrays, having O(n) time and O(1) space complexity. Pseudocode and a Python implementation are provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

RAIN WATER TRAPPING

Sabiha Barlaskar
PROBLEM STATEMENT
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.
EXAMPLE
EXAMPLE

Bars for input = 0,1,0,2,1,0,1,3,2,1,2,1


Total trapped water = 1 + 1 + 2 + 1 + 1 = 6
BRUTE FORCE METHOD
Initialize max_left = 0 and max_right=0
Iterate from the current element to the beginning of array updating:
max_left=max(max_left,height[j])
Iterate from the current element to the end of array updating:
max_right=max(max_right,height[j])
Add min(max_left,max_right)height[i]to water
Time complexity: O(n^2) )
Space complexity: O(1)O(1) extra space.
DYNAMIC PROGRAMMING
Find maximum height of bar from the left end up to an index
i in the array left_max
Find maximum height of bar from the right end up to an
index i in the array right_max
Iterate over the height array and update water
Add min(max_left[i],max_right[i])height[i] to water
EXAMPLE

0
0 1 0 2 1 0 1 3 2 1 2 1
LeftMax 0 1 1 2 2 2 2 3 3 3 3 3

RightMax 3 3 3 3 3 3 3 3 2 2 2 1

Min(L,R) 0 1 1 2 2 2 2 3 2 2 2 1

Min(L,R) -
Block[i]
0 0 1 0 1 2 1 0 0 1 0 0
CODE
height = [0,1,0,2,1,0,1,3,2,1,2,1]

def trap(height):

n = len(height)

leftHeight= [0] * n

rightHeight = [0] * n

water = 0

leftHeight[0] = height[0]

for i in range(1,n):

leftHeight[i] = max(height[i],leftHeight[i-1])

rightHeight[n-1] = height[n-1]

for i in range(n-2, 0, -1):

rightHeight[i] = max(height[i],rightHeight[i+1])

for i in range(1,n-1):

water += min(rightHeight[i], leftHeight[i]) - height[i] #Note, this is always non-negative.

return water

print trap(height)
LIMITATIONS OF DYNAMIC PROGRAMMING
Time complexity is O(n)
Space complexity is O(n)
USE 2 POINTERS
Algorithm
Initialize left pointer to 0 and right pointer to size-1
While left<right, do:
If height[left] is smaller than height[right]
If height[left]>=left_max, update left_maxleft_max
Else add left_maxheight[left] to ans
Add 1 to left.
Else
If height[right]>=right_max, update right_maxright_max
Else add right_maxheight[right] to ans
Subtract 1 from right.
PROGRAM CODE IN PYTHON
height = [0,1,0,2,1,0,1,3,2,1,2,1]
def trap(height):
left = 0
right = len(height) - 1
water = 0
left_max = 0
right_max = 0
while(left<right):
if height[left]<height[right]:
if height[left] >= left_max:
left_max = height[left]
else:
water += left_max - height[left]
left += 1
else:
if height[right]>=right_max:
right_max = height[right]
else:
water += right_max - height[right]
right -= 1
return water
print trap(height)
COMPLEXITY
Time complexity: O(n)
Space Complexity: O(1)
QUESTIONS??

You might also like