Trapping Rainwater Problem | Leetcode | Rain water trapping problem | DSA-One Course #16

Trapping Rainwater Problem | Leetcode | Rain water trapping problem | DSA-One Course #16


Trapping Rainwater Problem | Leetcode | Rain water trapping problem | DSA-One Course #16

Hey guys, In this video we’re going to solve a very famous Leetcode problem known as Rainwater trapping problem.

Solution with O(1) space can be found here: https://www.techiedelight.com/trappin
Assignment problems: https://www.techiedelight.com/Categor

Follow for updates:
Instagram: https://www.instagram.com/Anuj.Kumar
LinkedIn: https://www.linkedin.com/in/anuj-kuma
Telegram: https://t.me/coding_enthusiasts


DSA-One course:    • Time Complexity \u0026 Big O notation | An…  

Link to the code: https://www.geeksforgeeks.org/majorit
Practice problems: https://www.interviewbit.com/courses/

Use #DSAOne to spread knowledge and show your support on LinkedIn, Instagram.

Join the Telegram Group for DSA-One course: https://t.me/dsa_one


Ignore these tags:

trapping rainwater problem
trapping rain water
trapping rain water problem
trapping rainwater problem leetcode
trapping rainwater
problem solving
trapping rain water leetcode
trapping rainwater leetcode
trapping rainwater leetcode java
trapping the rainwater problem
trapping rain
rain water trapping problem
leetcode trapping rain
rainwater trapping
trapping water problem
rain water problem
trapping rain water problem explanation
rainwater problem
trapping rainwater problem
trapping rain water
trapping rain water problem
problem solving
trapping rain water leetcode
trapping rainwater leetcode
trapping rainwater leetcode java
trapping rainwater problem leetcode
trapping the rainwater problem
trapping rainwater
leetcode trapping rain
trapping rain
rain water trapping problem
trapping rain water problem explanation
rain water problem
rainwater problem
trapping rainwater leetcode python
trapping rainwater problem
trapping rain water
trapping rain water problem
trapping rainwater problem leetcode
trapping rainwater leetcode
trapping rainwater leetcode java
trapping water problem
trapping rainwater
rain water trapping problem
trapping rain water problem explanation
trapping rain water leetcode
rain water trapping problem using stack
trapping rain water solution
rainwater trapping
problem solving
rain water problem
rainwater problem

trapping rain water problem
trapping rain water
rain water trapping problem
trapping rainwater problem
rainwater trapping problem
anuj bhaiya
trapping rainwater
tapping rain water problem
trapping rainwater leetcode
trapping rain water leetcode
rain trapping problem
42. trapping rain water
water trapping problem
container with most water leetcode
container with most water
dsa problem solving
trapping water problem
rain water problem
rain water tapping problem
dsa question
rain water trapped
leetcode 42
dsa
rain water trapping
tapping rainwater
dsa one
rain water
rainwater
anuj bhaiya java
largest rectangle in histogram
leetcode solutions
trapping rain water problem in c++
anuj bhaiya dsa
leetcode
rain water preservation code
rain water trapping problem using stack
rainwater preservation code in java
trap rain water problem
trapping rain water problem in java
trapping rain water problem pepcoding
pepcoding
rain water preservation java code
trapping rainwater problem pepcoding
11. container with most water
data structure
love babbar
rainwater preservation java program
rainwater preservation program in java
tapping rain water
tapping rainwater problem
trapping rain water pepcoding
water trapping
anuj bhaiya resume
array problems
arrays in java
dsa questions
java dsa
leetcode trapping rain water
minimum swaps required to bring all elements less than or equal to k together
rain water preservation code in java
rainwater harvesting
rainwater preservation in java
trapping rain water 2
trapping water leetcode
anuj
array dsa
best time to buy and sell stock ii
best time to buy and sell stock leetcode
buy and sell stocks leetcode
chocolate distribution problem
data structures
dsa array
dsa java
maximum profit by buying and selling a share at most twice
maximum water between two buildings
rainwater preservation java code
stock buy and sell leetcode
stock span problem
trapping rain water java
trapping rain water neetcode
trapping rain water python
trapping rainwater problem using stack
trapping water
two pointer problems
water collection
water connection problem gfg
water jug problem in artificial intelligence


Content

0 -> Hey, what's up guys, Anuj here and
0.915 -> welcome to the DSAOne course.
1.958 -> In today's video we are going to solve
3.004 -> a very interesting problem which is rainwater trapping problem.
5.627 -> This is very common problem,
8.342 -> it has been asked a lot but
10.63 -> it uses a very good concept, which is
13.499 -> Array Preprocessing, we'll see about preprocessing.
16.471 -> Actually we've already seen preprocessing in
18.979 -> buy and sell stocks, we thought of way which is preprocessing
24.057 -> and we would be doing the same type of thing here
26.463 -> Let's understand the problem a little bit
28.234 -> in rainwater trapping problem,
29.159 -> you are given multiple buildings or blocks,
32.971 -> whatever you would like to call them,
34.342 -> their heights are given, say
38.578 -> it's height is 3, this one's 1, here is height 2, this is 4, 0 height, in this way.
45.393 -> they are standing, suppose on earth, in a 2d plane
50.976 -> and you have to drop water from above
52.616 -> it's raining and water is coming from above, okay.
54.671 -> So some of the rainwater will get trapped here
57.155 -> when the rain gets over. The water will get stored.
60.015 -> You have to tell how much water will be stored,
62.27 -> how much rainwater is getting trapped.
64.424 -> So now you must understand the name of this problem.
66.974 -> See, water will be stored here in this area,
70.731 -> it won't be stored above this because will get out then.
74.544 -> It won't store here, the water will just get out from.
78.391 -> Water will be stored where it's possible.
81.977 -> This block also has space so the water is here too,
86.558 -> and from here it will go out too. There's no water here.
89.652 -> So the water is stored in the area where I've marked it with red.
93.298 -> So you have to tell how much water is stored here.
95.64 -> You have to answer in blocks or units,
98.147 -> like 1 2 3, so the water is stored up to 3 blocks here,
101.765 -> and how much is here.
102.487 -> 1 2 3 4 5, so water is stored here up to 5 blocks.
106.057 -> So 5+3 will be 8.
107.987 -> So our total is 8, that is, 8 units of water will be trapped here.
112.23 -> So you have to tell how these 8 will come.
114.343 -> So this is a unique type of problem, very interesting problem.
117.687 -> Try to solve it.
118.751 -> I have given you a hint for preprocessing, that is going to be used here.
122.494 -> Means you have to preprocess this array and
125.941 -> you have to store it as an axiliary array.
127.899 -> So I've given you a big hint,
130.013 -> now try to solve it, and after that we'll go ahead.
132.432 -> If you guys would have thought,
133.636 -> you must have wondered
136.029 -> where the water is getting stored.
137.418 -> If we go to every index, and look at any index
139.792 -> water will get stored in an index
141.547 -> when its previous and its next block are higher than it.
145.612 -> Then water can be stored.
147.317 -> Can water be stored here?
149.059 -> It cannot because there's no higher block before it.
153.562 -> There is one after it but there's no block before it.
159.11 -> So in this index water cannot be stored,
162.043 -> you can try it for everywhere, like why is water getting stored here.
165.429 -> Because before this there is a building
171.836 -> and to it's right there is a building.
174.056 -> That's why water is getting stored here.
175.632 -> You can check anywhere, here there's no storage of water
177.567 -> because there's no building to its left or right
179.69 -> higher than it is.
181.086 -> Why here is no water storage, there is a building higher to its left
183.951 -> but not to its right.
185.296 -> And the same is here, there is no building to its right.
188.414 -> So you have understood one thing
190.652 -> if we want to know where is the water getting stored then
193.007 -> all we have to see is there must be a building to its left
196.123 -> to its whole left, which is bigger than it,
200.259 -> and a building higher than it to its right.
202.699 -> So we understood that, now we have to know
205.159 -> how much water is getting stored on every index.
207.757 -> What can be the formula for that. Think about that.
210.387 -> Now we are getting into preprocessing, how?
212.71 -> What I'll do is check for every element which is the building higher than it to its left.
217.756 -> So I will make an auxiliary array, or left array.
222.542 -> Which will tell about which building has the maximum height to its left.
229.775 -> There's no building before it, so its height will be taken, 3.
234.186 -> So this will be 3.
235.259 -> Now before this, we will see the maximum between these two,
239.058 -> so obviously between these 3 is bigger, so there's a
241.491 -> building before this whose height is 3.
244.363 -> So here it will be 3.
245.277 -> Then we'll come here and between 2 and 3
247.738 -> 3 is the higher one so there's a building with height 3 before it
252.181 -> Now here, 4 is bigger between 4&3, so
254.206 -> 4 will be written here.
255.743 -> Than between 0 and 4, there's a building before 0 which is 4.
261.326 -> Between 1&4, 4 is still bigger.
264.318 -> and in the rest 2 and 3, 4 is the higher one.
268.352 -> So we've created a left array, similarly
270.559 -> we will make an array for right side too because
272.359 -> we want to check both of the sides, right.
273.733 -> Let's make an array for right side, so
275.257 -> for this array, I'll call it right.
278.678 -> And we will start filling it from the right side.
280.513 -> Now we see, which is the bigger one to its right
284.069 -> so this is the biggest, so this will be 2.
287.688 -> Now between 2&3, which is bigger
288.993 -> its height is 3 and its the higher one. So this will be 3.
295.318 -> Now here, between 1&3, so obviously 3 is the bigger one.
299.343 -> There's a building to its right side whose height is 3.
301.721 -> Between 0&3, who's higher, 3, so
306.167 -> basically in every index, the value is telling us
308.471 -> that to its right, because this array's name is right,
311.132 -> which building has the highest height or is maximum.
317.857 -> Here, between 4&3, 4 is the bigger one
321.171 -> in 2&4, 4 is the bigger one, then again 4(x2).
327.743 -> So we have created two arrays, left and right,
330.638 -> these arrays are different from this array
333.305 -> using O(n) space we have created these arrays.
336.179 -> So we have created this.
337.956 -> Now our work is to go to every index,
340.111 -> how much water can be trapped there?
341.836 -> Now we will apply a formula here
343.778 -> and that formula basically is,
344.789 -> on any index, that is on any position, we will
347.915 -> check the maximum height to its left and right, and the one
352.067 -> which is smaller of the two, will be the unit of water store able in them.
354.349 -> How?
354.966 -> If you come here, the maximum height to its left is 3
359.882 -> and to the right is 4, okay.
362.165 -> This is the 3 building, and this one's 4 to its right.
365.984 -> Between them, the one who's smaller will be how much water
368.239 -> storable in that position, right.
369.323 -> So between 4&3, 3 is the smaller, and it is the amount of water that can be stored.
372.262 -> We can even see, if the water tried to hold 4 unit of water then it would just spill out.
377.808 -> Smaller one between 4&3 is 3 which is the amount of water it is able to hold.
384.709 -> But there's also a building at the bottom, so this
387.991 -> much amount of water will not be stored.
392.838 -> So we have got the formula which is
394.479 -> the one which is smaller between left and right,
396.972 -> take that and subtract the height of the building on it.
400.827 -> This will tell us how much water
404.251 -> can be stored on that index or block or unit.
406.295 -> So the formula is, so formula for any index 'i', is
415.446 -> Math.min(left(i),right(i)) - a(i)
429.378 -> So for any index (i) the formula is
431.364 -> take the minimum from left and right and subtract the height of the building, and that is the amount of water stored able.
437.555 -> Like here, start from this one, for this its 3&4, the smaller is 3,
444.74 -> subtract 3 from 3, you'll get zero so no water is being held here.
449.604 -> Water held is 0.
452.811 -> Here, 4&3, smaller one is 3, subtract the height from it,
456.612 -> 3 - 1 which is 2. So the water being held here is 2.
460.993 -> Here, between 3&4, smaller is 3, 3 - (height)2 which is
466.384 -> 1. So the water held here is 1 block.
469.817 -> Between 4&4, smaller is 4, 4 - 4 = 0, so here is zero.
474.975 -> Smaller of 4&3 is 3, 3 - 0 which is 3,
480.31 -> so 3 blocks of water is stored here.
483.014 -> Now here, 4&3, smaller is 3, 3 - 1 which is 2,
489.217 -> so two blocks of water is stored here.
491.634 -> Let's see here, between 4&3, 3 is smaller, 3 - 3 which is 0.
494.926 -> Between 4&2, 2 is smaller, 2 - 2 which is 0.
497.973 -> Now you will add these numbers, 2 + 3 which is 5,
501.481 -> 5 + 1 which is 6, 6 + 2 which is 8.
504.865 -> So now we know that 8 blocks of water is being stored here.
508.994 -> So see it's very simple, the code is going to be very easy.
511.849 -> Let me code this and then you will understand it better,
514.497 -> it's going to a simple code, you guys can see.
516.782 -> Two auxiliary arrays are made here, left and right and
520.825 -> we solve using those and this is the formula used.
524.849 -> So this is going to be a very simple code,
526.646 -> you guys can try it yourself.
528.103 -> Otherwise I am doing it.
528.856 -> Okay, I have coded this, its got a bid code actually,
532.229 -> there wasn't space below so I wrote it there.
536.207 -> So same logic that you guys saw, I've written the same logic.
540.109 -> First we get an array, which represents heights of the blocks.
544.623 -> this is 'n', length of the array,
546.737 -> I've created two arrays, one left and another one right.
549.227 -> We have initialized them with the same length,
552.2 -> then left(0) will be a(0), we have assumed
554.738 -> the first position to be a(0).
557.141 -> And I've filled the left array in this way.
559.777 -> So left[i] will tell the index of any position of the left,
563.311 -> i - 1 means the maximum from the current and previous element will come here.
568.376 -> Similarly we have filled the right array, right[n - 1] = a[n - 1],
572.031 -> means the last position in right will be the same as the position of the element.
575.085 -> And we are going from the back to the start in array,
577.811 -> from i=n - 2, until i becomes greater than or equal to 0.
582.122 -> So right i will basically be Math.max of right i + 1,
586.455 -> which is just next element, and the maximum from the current element.
591.101 -> So our left and right array have been filled, and
593.911 -> now we have created a variable 'ans'
596.592 -> in which our final answer will be shown.
598.312 -> And I've filled it using the formula I told you.
603.084 -> So we are going from 0 to n,
605.337 -> math.min of, if we want to tell how muny blocks of water
608.774 -> is on any index, for that we have to get
611.593 -> minimum of left(i) and right(i), and subtract a(i),the height, from it.
617.797 -> And the result of that will be added into answer and return that answer
621.66 -> And we would be done here.
622.958 -> So this is a big code but a simple one, you can code this easily in 2 minutes.
628.451 -> With this, the problem is solved and most of our problems in array are solved.
634.004 -> After this we will go ahead with sorting in arrays,
636.86 -> so I hope you like this video, like this video, subscribe to the channel, and I'll see it in the next video.
646.268 ->

Source: https://www.youtube.com/watch?v=UZG3-vZlFM4