42. Trapping Rain Water | Popular interview problem Leetcode | Competitive programming | CityCoder

preview_player
Показать описание
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.
Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6

The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
Рекомендации по теме
Комментарии
Автор

Thanks for the explanation. Case anyone is wondering how to do this in Python, here is my approach:

def trapped_water(heights):
volume_trapped = 0

for i in range(1, len(heights)-1):
max_left = max(heights[:i+1])
max_right =

volume_trapped = volume_trapped + min(max_left, max_right) - heights[i]

return volume_trapped

debackerr
Автор

Very helpful but you should discuss the optimal approach
O(n) too.

shobhitranjan
Автор

Line 24, you should be careful while subtracting height, ex. [1, 10, 4]
Left = 1
Right = 4
Sum = sum + min(1, 4) - 10 [x]
i.e sum = sum - 9 [x]

shubhamrane
Автор

Please also solve Trapping Rain Water II. Thanks

vaishalirockzz
Автор

Thank you! The clarity in explanation helped me solve this problem.

sushavinu
Автор

thank you for helping me to solve one simple logical error in my code

dokodiaries
Автор

Good for understanding the problem though use 2 pointers for optimal solution.

ansumandas
Автор

You're good bro. Keep it up, best of luck!

abhigyanshrivastava
Автор

You elaborated so well. Thanks for sharing

diyakukreja
Автор

Thank you sir! U saved someone's GPA!!

khashbatenkhbat
Автор

You explained it very well... helpfull for students 👍

foodandme
Автор

Tq broo nice explaination in less time 🙏🙏 continue this

bisatisrilatha
Автор

Hi, what camera are you doing to shoot the video? It's a very good video!

techzoo
Автор

very helpful __but last 2 test case not passes
memory limit exceeded

RajanSingh-xrcf
Автор

Great Video Simple and Easy to understand ! Keep it up bro 👍

snehal
Автор

Thanks For giving solution in this short video

ShubhamKumar-mbsm
Автор

Bro when to use INT_MIN and INT_MAX??

bisatisrilatha
Автор

What is the time complexity of this code..
I think it is o(n2)..

jyothiprakashpatnaikuni
Автор

very nice explanation..thank you for this video

dhruvpatel
Автор

But why does this work? Makes no sense why this would be the solution

mariokrstevski
join shbcf.ru