LeetCode Container With Most Water Explained - Java

Sdílet
Vložit
  • čas přidán 6. 09. 2024
  • The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
    Join my free exclusive community built to empower programmers! - www.skool.com/...
    Preparing For Your Coding Interviews? Use These Resources
    --------------------
    (My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.d...
    AlgoCademy - algocademy.com...
    Daily Coding Interview Questions - bit.ly/3xw1Sqz
    10% Off Of The Best Web Hosting! - hostinger.com/...
    Follow Me on X/Twitter - x.com/nickwhit...
    Follow My Instagram - / nickwwhite
    Other Social Media
    ----------------------------------------------
    Discord - / discord
    Twitch - / nickwhitettv
    TikTok - / nickwhitetiktok
    LinkedIn - / nicholas-w-white
    Show Support
    ------------------------------------------------------------------------------
    Patreon - / nick_white
    PayPal - paypal.me/nick....
    Become A Member - / @nickwhite
    #coding #programming #softwareengineering

Komentáře • 52

  • @ucheanonyai7522
    @ucheanonyai7522 Před 4 lety +45

    I think you should put more effort into explanations by getting a white board or something...you just type out the code

    • @Rob-J-BJJ
      @Rob-J-BJJ Před rokem +4

      ungrateful

    • @aniruddhlaharia1275
      @aniruddhlaharia1275 Před 2 měsíci +1

      @@Rob-J-BJJHow is he ungrateful? He has a valid point, if he wants to teach something that requires visualisation it only makes sense that he uses a white board or some kind of markings to explain to his viewers and that's what everyone does on youtube, not just him.

  • @mostinho7
    @mostinho7 Před 4 lety +13

    Done thanks
    Two pointers, start off at the edges, move the pointer inward of the smaller edge. Doesn’t need to be O(n^2) because once u start off at the edges, no matter what you do, the width will always be decreasing (you started off at the edges so you have to move inwards) the only way to get a bigger area is if you keep the largest of the two edges you’re at and try to find an edge that will give u a greater area.

  • @somyasrivastava6315
    @somyasrivastava6315 Před 3 lety +6

    This is amazing Nick...very simple to understand, your explanation made this question look really simple. Nice

  • @munjal55555
    @munjal55555 Před 4 lety +2

    great explanation Nick!! , just one question , why we do not consider betn index 1 to index 6 : height of 1. so what I mean is the answer should be 49 + 4 = 53. because betn 8 and 8 their is still a bar if height 1 we can fill. and now it becomes max.

  • @InfoBuzz1130
    @InfoBuzz1130 Před rokem

    Why are you not checking else if(height[bpointer] < height[apointer]) instead just else, because if add a else condition am getting a timeoutexception and not sure why on leetcode

  • @thatguy14713
    @thatguy14713 Před 3 lety +4

    You haven't explained the intuition behind the solution, rather you have memorized the solution and regurgitated without understanding it. You also have an unnecessary repetition of the calculation of max_area, it can be done just once outside of the if statement.

  • @jessiegu4853
    @jessiegu4853 Před 4 lety +3

    wow! You rock Nick! Simple and clear!

  • @georgechen1124
    @georgechen1124 Před 4 lety +7

    well done Nick! Publish more solutions pls!!

    • @abishekkachroo938
      @abishekkachroo938 Před 4 lety

      If you get it
      Can you please explain line no.9
      height[a_pointer] *[b_pointer - a_pointer];
      Means shouldn't we be multiply height[] * height[] instead of this

    • @mrfred456123
      @mrfred456123 Před 4 lety

      @@abishekkachroo938 No we should not as we have to find the area of the rectangle and you are multiplying length * length
      while it should be length*breadth (given in video )...the distance between two pointers is the breadth.

  • @jagdishwarbiradar1763
    @jagdishwarbiradar1763 Před 4 lety +1

    hey , is this a same problem as the largest histogram ,if yes then why the solution this can't apply it to largest histogram pr and it didn't work?why?

    • @iark1
      @iark1 Před 4 lety

      Because histogram blocks will take some volume out of the water. So we need to subtract them. In the problem above, the bars are taking no volume.

  • @ricocode
    @ricocode Před 2 lety

    How do we know pointers are at idx 0 and len(height)? Is it possible to get a working solution w/ i = 0, j + 1. If not, why? Thanks!

  • @sayantaniguha8519
    @sayantaniguha8519 Před 2 lety

    What is the difference b/w this Q and *Largest Rectangle in Histogram* ?

  • @dhawaldhingra
    @dhawaldhingra Před 2 lety

    How do you handle the case when the heights of a_pointer and b_pointer are same? Does it not matter which pointer we move in such a case?

    • @shaileshsingh9218
      @shaileshsingh9218 Před 2 lety

      You will have the move both the pointers in that case, and if you aren't explicitly coding this case, both the pointers will eventually move in the further iterations to check if you have a larger area possible or not.

  • @satyamgupta6030
    @satyamgupta6030 Před rokem

    once again nick bro explaining the problem and approach very clearly.
    Thanks nick brother u help me alot in learning these concepts and approaches

  • @alitherland765
    @alitherland765 Před 2 lety

    Why does sliding window not work here?

  • @lifeofme3172
    @lifeofme3172 Před 2 lety

    Hi dont understand why we use small pointer.

  • @FF-ne2qz
    @FF-ne2qz Před 5 lety +18

    Ok, why this approach is correct?

    • @rz5505
      @rz5505 Před 4 lety +77

      Because the width is always decreasing. The only way to get a larger area is to increase the height of the rectangle. And since the height is determined by the shorter end, you move the shorter end in the hope of increasing the height of the rectangle.

    • @saimraza8342
      @saimraza8342 Před 4 lety +26

      @@rz5505 This comment is better than watching the whole video. The video doesn't give any intuition.

    • @tonyz2203
      @tonyz2203 Před 4 lety +2

      @@saimraza8342 I agree

    • @raytruong5882
      @raytruong5882 Před 4 lety +4

      @@rz5505 This comment is more helpful than anything I've seen on the leetcode submissions or endless videos explaining it

    • @RaveWave5506
      @RaveWave5506 Před 4 lety +4

      These kidos throw at you the solution that they have seen in some discussion without any thought process. Even if you go to the interview and you vomit it. If you can't explain to the interviewer very well, in clear formal terms how you found it and why is it correct, you will get no points.

  • @nerodant85
    @nerodant85 Před 2 lety

    Thank you for the vids Nick, they really help 🙏

  • @damudaran3765
    @damudaran3765 Před 4 lety

    why he have written bpointer - apointer pls someone tell.

    • @axbax93
      @axbax93 Před 4 lety +1

      if apointer is 2 and bpointer is 5, the width would be 5-2 = 3; thus bpointer - apointer to get the width for area

  • @Meenereem
    @Meenereem Před 10 měsíci

    Thank you!

  • @jugsma6676
    @jugsma6676 Před 4 lety +1

    This is my solution, python version:
    def find_max_container(container):
    if len(container) == 0 or len(container) == 1:
    return 0
    min_ht = min(given[0], given[1])
    max_vol = min_ht * 1
    for step in range(2, len(container)):
    for i in range(step):
    min_ht = min(given[i], given[step])
    vol = min_ht * (step-i)
    if max_vol < vol:
    max_vol = vol
    return max_vol

  • @shoaibali4309
    @shoaibali4309 Před 4 lety

    thank you, sir.

  • @greatred2558
    @greatred2558 Před 2 lety

    thanks

  • @erictsang0
    @erictsang0 Před 2 lety

    Lmao damn, and here i was trying to use branch n bound ...

  • @Entertaiment532
    @Entertaiment532 Před rokem

    good

  • @pietrocoppe4796
    @pietrocoppe4796 Před 3 lety

    life is wonderful, smile!

  • @oneworldonepath
    @oneworldonepath Před 4 lety

    Why 8*8 =64 is not the max but 49?

    •  Před 4 lety +13

      I think you misunderstood the task problem. Read it one more time to really understand it.
      You can't choose 8 and 8, because then you are choosing height 8 at index 1 and another height 8 at index 6. So the area between heights 8 and 8 would then be 8 * (6-1) = 40.
      But if we choose height 8 at index 1 and height 7 at index 8, then the area between heights 8 and 7 is 7 * (8 - 1) = 7 * 7 = 49 which is larger than 40.
      The task is to calculate max area between two lines, not just multiplying two heights.

    • @oneworldonepath
      @oneworldonepath Před 4 lety

      @ Got it now... Thanks for replying 🙂👍

    •  Před 4 lety +1

      @@oneworldonepath Glad to help! 😊

    • @plzdntsndmestuff
      @plzdntsndmestuff Před 3 lety

      @ This explanation was so helpful.

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

    That what happens when you memorize the solution and vomit it out without thinking.
    and even though u solved it one hour ago, first try failed. Grow up dude

  • @sayantaniguha8519
    @sayantaniguha8519 Před 2 lety

    What is the difference b/w this Q and *Largest Rectangle in Histogram* ?