Trapping Rain Water || Best optimization || O(N) Time O(1) space || Intuition with Code and Example
Vložit
- čas přidán 4. 09. 2021
- Given an array arr[] of N non-negative integers representing the height of blocks. If width of each block is 1, compute how much water can be trapped between the blocks during the rainy season.
Input:
N = 6
arr[] = {3,0,0,2,0,4}
Output:
10
Explanation:
oh god, you explained better than striver. hats off. Once you said the word limiting, i understood the whole algo and coded it myself.
true,better then big youtube channels
Yaa🔥🔥
true!! limiting word helped : )
i also came after watching striver..you explained better
Exactly !! Was working on the problem for hours ! LIMITING was the word that made everything clear !!
You used the right words to build intuition and everything seemed so simple!!!! Thanks a lot
It never happened that I came to understand the solution from your video and not understand it. Your intution and approach are so easy to understand. Thanks and please continue this great work.
Finally got an approach with constant space. Best explanation!!
Hats off to your efforts.. You're the most underrated educator on this platform 🙏🙏
This was the best explanation so far, concepts are clear! Thanks a bunch🥺 Your channel is so underrated :(
Thank you so much for this explanation, I had solved it with 2 arrays, was struggling alot to understand the O(1) space logic. The words you used really helped to unlock that link.
oh my god,such a nice explanation .
its not just this question ,when no one is able to make me understand the solution ,its always your solution which help me understand .
Even this video is two year old and I am watching it right now, after watching multiple other video solutions ,I can confidently say that your explanation is the best out of all I have watched till now. This video is ever green. Thanks for your effort. Please Don't quit from making this type of video, keep it up at least for your viewers. The only word I can say about this video is "Excellent".
Thanks for the detail explanation. Now I got to understand the intuition behind using the two pointer approach to this problem
Such an easy and lucid explanation. One of best explanation..Kudos..🙂
Best and easy explanation have ever seen. Thank you
Very nice and fast Explaination.Good Concentration till last minute.
Thanks for the explanation and the efforts you put in making things simpler in this video
Nice explanation with such a clean code!
Wonderfull explanation,thank u very much 🙂🙂
Very nice explanation literally.
You just gained one subscriber.
Beautiful
thanks again,learning important problems from ur channel
U are doing great job 🤩thankyou so much for sharing your knowledge.please also make a video on your interview questions
Really underestimated teacher you are 🎉😮 Awesome 💯 I had watched almost 7 videos of you since morning 🌅 Exclaimed Explained
though I saw this approach many times but couldn't understand before so keep looking for more. Only, in this video, I could actually feel that I understood the concept. Thanks and keep it up. 👌👌👏👏
the explanation is to clean to understand the problem and also simple. thanks
thanks for this great video and amazing explanation
your explanation for the problem are greater ,thank you for the video, it was very useful to me
Thank you so much ma'am for such a great Explanation😍
Thanks for the great explanation
great explanation , you deserve more likes and suscribers....
Really, it is awesome 🔥
Keep it up .
Thankyou.. great explanation really😊
Must say you are simply awesome !!!!!!
nice explanation than striver greatttttt
Great explanation. Subscribed
very intutive explanation brilliant👑👑👑👑👑👑
Best video for this problem!
thankuu so much dear🥰.
amazing explanation
best video thanks. i understood
20:04 Uncle ji parnam XD,
great explanation btw!
Great explanation 🔥
Brilliant!
Greatest explanation ❤🎉 tnx
Uncle is extremely cute btw :). Great video though Alisha :)
nice explanation and if you don't mind could you please reduce the screen size (zoom out ) for all your videos so I can able to see whole code (screen)
and also please make a video on how to maintain the consistency (problem solving ) 😊
great Alisha thank you
nice explanation
Perfect😍
Thanks 👍🙏
Nice explaination, Can you please create a video for time management, like the sleep crisis during preparation, It feels like a sleep deprivation is on the way, please help tackling.🙂
when you said limiting bar decide ans that point, game is over👍
amazing skills.. thank you, could you please post some content on linkedin as well?
Thank a lot ☺
Thank you!!!!!
its very helpful thanku
thank you
Thank you ma'am.
ThankYou mam!!!
your intuition is the best!
Actually, ur code could have been more intuitive if u calculated left max and right max and then did leftmax
Thanks mam
you strudied from striver and explained bettter than him
very well alisha
Thanks!!
Nice mam. It was quite good solution.😇😇
Mam plz upload leetcode daily solution too.🙏🙏
It really help a lot..
Can u explain on placement problems of java
tq mam 🤗
i love your way n u
class Solution {
public int trap(int[] height) {
if (height == null || height.length == 0) {
return 0;
}
int left = 0; // Initialize left pointer
int right = height.length - 1; // Initialize right pointer
int leftMax = height[left]; // Maximum height to the left
int rightMax = height[right]; // Maximum height to the right
int ans = 0; // Variable to store the total water trapped
while (left < right) {
if (leftMax < rightMax) {
// If leftMax is less than rightMax, process the left side
left++;
leftMax = Math.max(leftMax, height[left]);
ans += Math.max(0, leftMax - height[left]);
} else {
// If rightMax is less than or equal to leftMax, process the right side
right--;
rightMax = Math.max(rightMax, height[right]);
ans += Math.max(0, rightMax - height[right]);
}
}
return ans;
}
}
the space complexity is O(n) not O(1) as you mentioned it in thumbnail please change it
we are not using any array or stack so how the sc is o(n)
491 likes are too less for your explnation!
alisha ,u r a gud girl