Trapping Rain Water || Best optimization || O(N) Time O(1) space || Intuition with Code and Example

Sdílet
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:

Komentáře • 78

  • @jatinbhatoya8420
    @jatinbhatoya8420 Před 2 lety +58

    oh god, you explained better than striver. hats off. Once you said the word limiting, i understood the whole algo and coded it myself.

    • @piyushsingh9320
      @piyushsingh9320 Před 2 lety +8

      true,better then big youtube channels

    • @yeswanthh5068
      @yeswanthh5068 Před rokem +4

      Yaa🔥🔥

    • @anandyadav_
      @anandyadav_ Před rokem +4

      true!! limiting word helped : )

    • @lostcyrus8578
      @lostcyrus8578 Před rokem +3

      i also came after watching striver..you explained better

    • @radhikagokani3660
      @radhikagokani3660 Před rokem +1

      Exactly !! Was working on the problem for hours ! LIMITING was the word that made everything clear !!

  • @hema4266
    @hema4266 Před 6 měsíci +2

    You used the right words to build intuition and everything seemed so simple!!!! Thanks a lot

  • @RassieVanderDussen-mc8um

    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.

  • @krishnasahay9694
    @krishnasahay9694 Před rokem +5

    Finally got an approach with constant space. Best explanation!!

  • @SirAkPandey
    @SirAkPandey Před rokem +7

    Hats off to your efforts.. You're the most underrated educator on this platform 🙏🙏

  • @aishwaryakulkarni9452
    @aishwaryakulkarni9452 Před 11 měsíci +1

    This was the best explanation so far, concepts are clear! Thanks a bunch🥺 Your channel is so underrated :(

  • @bhargavinaik8145
    @bhargavinaik8145 Před 4 měsíci

    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.

  • @anonXoXoXo
    @anonXoXoXo Před 5 měsíci

    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 .

  • @b2aquantschool798
    @b2aquantschool798 Před 4 měsíci

    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".

  • @naredlaaalana9371
    @naredlaaalana9371 Před 4 měsíci

    Thanks for the detail explanation. Now I got to understand the intuition behind using the two pointer approach to this problem

  • @raufurkhan9191
    @raufurkhan9191 Před rokem +3

    Such an easy and lucid explanation. One of best explanation..Kudos..🙂

  • @harish5466
    @harish5466 Před rokem

    Best and easy explanation have ever seen. Thank you

  • @rohithjakkani8343
    @rohithjakkani8343 Před rokem

    Very nice and fast Explaination.Good Concentration till last minute.

  • @addankihasith4672
    @addankihasith4672 Před 6 měsíci

    Thanks for the explanation and the efforts you put in making things simpler in this video

  • @rudra-18
    @rudra-18 Před rokem +1

    Nice explanation with such a clean code!

  • @yeswanthh5068
    @yeswanthh5068 Před rokem +1

    Wonderfull explanation,thank u very much 🙂🙂

  • @KunalSharma-cj5hc
    @KunalSharma-cj5hc Před 2 lety

    Very nice explanation literally.
    You just gained one subscriber.

  • @rabindrapatra7151
    @rabindrapatra7151 Před 27 dny

    Beautiful

  • @bharathkumar5870
    @bharathkumar5870 Před 2 lety

    thanks again,learning important problems from ur channel

  • @techiestart3233
    @techiestart3233 Před 2 lety

    U are doing great job 🤩thankyou so much for sharing your knowledge.please also make a video on your interview questions

  • @kumaravinashsinghabnish4021
    @kumaravinashsinghabnish4021 Před 9 měsíci

    Really underestimated teacher you are 🎉😮 Awesome 💯 I had watched almost 7 videos of you since morning 🌅 Exclaimed Explained

  • @rinkygarg7350
    @rinkygarg7350 Před 4 měsíci

    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. 👌👌👏👏

  • @avijitmaity7436
    @avijitmaity7436 Před 2 lety

    the explanation is to clean to understand the problem and also simple. thanks

  • @Yash-fh7gm
    @Yash-fh7gm Před rokem

    thanks for this great video and amazing explanation

  • @jagan2350
    @jagan2350 Před rokem

    your explanation for the problem are greater ,thank you for the video, it was very useful to me

  • @gauravverma2901
    @gauravverma2901 Před rokem

    Thank you so much ma'am for such a great Explanation😍

  • @sudeepgupta57
    @sudeepgupta57 Před měsícem

    Thanks for the great explanation

  • @bikramshahi8512
    @bikramshahi8512 Před 2 lety

    great explanation , you deserve more likes and suscribers....

  • @ashutoshlawaniya9833
    @ashutoshlawaniya9833 Před 2 lety

    Really, it is awesome 🔥
    Keep it up .

  • @gitanjalikumari9262
    @gitanjalikumari9262 Před 9 dny

    Thankyou.. great explanation really😊

  • @ashishtiwari6000
    @ashishtiwari6000 Před 2 lety

    Must say you are simply awesome !!!!!!

  • @sheraurshayri2033
    @sheraurshayri2033 Před 7 měsíci

    nice explanation than striver greatttttt

  • @ManishKumar-jz2jr
    @ManishKumar-jz2jr Před rokem

    Great explanation. Subscribed

  • @charand9597
    @charand9597 Před rokem

    very intutive explanation brilliant👑👑👑👑👑👑

  • @arshdeep011
    @arshdeep011 Před rokem

    Best video for this problem!

  • @ankit80354
    @ankit80354 Před rokem

    thankuu so much dear🥰.

  • @adityaband6919
    @adityaband6919 Před 11 měsíci

    amazing explanation

  • @Mahmmadhusen072
    @Mahmmadhusen072 Před 2 lety

    best video thanks. i understood

  • @drizzle01
    @drizzle01 Před 11 měsíci

    20:04 Uncle ji parnam XD,
    great explanation btw!

  • @lakshyakumawat9710
    @lakshyakumawat9710 Před rokem

    Great explanation 🔥

  • @bishaladhikari9499
    @bishaladhikari9499 Před rokem

    Brilliant!

  • @priyankpatel1580
    @priyankpatel1580 Před rokem

    Greatest explanation ❤🎉 tnx

  • @abhishekkarn8918
    @abhishekkarn8918 Před měsícem

    Uncle is extremely cute btw :). Great video though Alisha :)

  • @nagendrapp2213
    @nagendrapp2213 Před 2 lety

    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 ) 😊

  • @narendra7556
    @narendra7556 Před 2 lety

    great Alisha thank you

  • @kushalshukla444
    @kushalshukla444 Před rokem

    nice explanation

  • @danussh
    @danussh Před 2 lety

    Perfect😍

  • @ayushsinghrathore360
    @ayushsinghrathore360 Před rokem +1

    Thanks 👍🙏

  • @venkataramanan6354
    @venkataramanan6354 Před rokem

    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.🙂

  • @expert_solver
    @expert_solver Před rokem

    when you said limiting bar decide ans that point, game is over👍

  • @nasirkhansirajbhai8016

    amazing skills.. thank you, could you please post some content on linkedin as well?

  • @ProgBoost
    @ProgBoost Před rokem

    Thank a lot ☺

  • @Alan-bu2hi
    @Alan-bu2hi Před 6 dny

    Thank you!!!!!

  • @maddycoder1294
    @maddycoder1294 Před rokem

    its very helpful thanku

  • @raedhasan2240
    @raedhasan2240 Před 11 měsíci

    thank you

  • @SleepeJobs
    @SleepeJobs Před 2 lety

    Thank you ma'am.

  • @shivanshgupta1509
    @shivanshgupta1509 Před 2 lety +1

    ThankYou mam!!!

  • @brijrajsinhgohil6576
    @brijrajsinhgohil6576 Před rokem

    your intuition is the best!

  • @satyajeetkumarjha1482
    @satyajeetkumarjha1482 Před 2 lety +1

    Actually, ur code could have been more intuitive if u calculated left max and right max and then did leftmax

  • @user-sm7zo5zd9t
    @user-sm7zo5zd9t Před 2 měsíci

    Thanks mam

  • @rakshitbro7601
    @rakshitbro7601 Před 29 dny

    you strudied from striver and explained bettter than him
    very well alisha

  • @shubhamsukum
    @shubhamsukum Před rokem

    Thanks!!

  • @sharadkumar6658
    @sharadkumar6658 Před rokem +2

    Nice mam. It was quite good solution.😇😇

    • @sharadkumar6658
      @sharadkumar6658 Před rokem

      Mam plz upload leetcode daily solution too.🙏🙏
      It really help a lot..

  • @saveethanotessharingsharee1155

    Can u explain on placement problems of java

  • @AnoopSingh-hx4wm
    @AnoopSingh-hx4wm Před rokem

    tq mam 🤗

  • @devadityasati7757
    @devadityasati7757 Před rokem

    i love your way n u

  • @SelfImprovementtip
    @SelfImprovementtip Před 2 měsíci

    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;
    }
    }

  • @prajjwaldubey5787
    @prajjwaldubey5787 Před 7 měsíci

    the space complexity is O(n) not O(1) as you mentioned it in thumbnail please change it

  • @youtubechannel-ub1xr
    @youtubechannel-ub1xr Před 3 měsíci

    491 likes are too less for your explnation!

  • @vinamrasangal8436
    @vinamrasangal8436 Před rokem

    alisha ,u r a gud girl