LeetCode Container With Most Water Explained - Java
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
I think you should put more effort into explanations by getting a white board or something...you just type out the code
ungrateful
@@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.
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.
thank u
This is amazing Nick...very simple to understand, your explanation made this question look really simple. Nice
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.
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
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.
wow! You rock Nick! Simple and clear!
well done Nick! Publish more solutions pls!!
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
@@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.
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?
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.
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!
What is the difference b/w this Q and *Largest Rectangle in Histogram* ?
yes, both of them have the same logic
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?
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.
once again nick bro explaining the problem and approach very clearly.
Thanks nick brother u help me alot in learning these concepts and approaches
Why does sliding window not work here?
Hi dont understand why we use small pointer.
Ok, why this approach is correct?
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.
@@rz5505 This comment is better than watching the whole video. The video doesn't give any intuition.
@@saimraza8342 I agree
@@rz5505 This comment is more helpful than anything I've seen on the leetcode submissions or endless videos explaining it
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.
Thank you for the vids Nick, they really help 🙏
why he have written bpointer - apointer pls someone tell.
if apointer is 2 and bpointer is 5, the width would be 5-2 = 3; thus bpointer - apointer to get the width for area
Thank you!
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
thank you, sir.
thanks
Lmao damn, and here i was trying to use branch n bound ...
good
life is wonderful, smile!
Why 8*8 =64 is not the max but 49?
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.
@ Got it now... Thanks for replying 🙂👍
@@oneworldonepath Glad to help! 😊
@ This explanation was so helpful.
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
What is the difference b/w this Q and *Largest Rectangle in Histogram* ?