Largest rectangle in Histogram | Leetcode #84
This lecture explains a very popular programming interview question which is to find the largest rectangle in histogram.Given an array representing heights of each bar in a histogram, we are required to find the area of the largest rectangle which can be formed from the given histogram.I have first explained the problem using some insightful examples and then i have shown the most important observation for this problem.Using this observation, I have shown an algorithm with optimization using stack which can find the largest rectangle area in the histogram in just O(N) time using just 3 traversals.I have also shown the dry run of the code along with code explanation at the end of the video.CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
========================================================================
Join this channel to get access to perks:
/ @techdose4u
INSTAGRAM : / surya.pratap.k
SUPPORT OUR WORK: / techdose
LinkedIn: / surya-pratap-kahar-47b...
WEBSITE: techdose.co.in/
TELEGRAM Channel LINK: t.me/codewithTECHDOSE
TELEGRAM Group LINK: t.me/joinchat/SRVOIxWR4sRIVv5...
=======================================================================
CODE LINK: gist.github.com/SuryaPratapK/...
USEFUL LINKS:-
Implement min stack: • Implement min stack | ...
Implement stack using heap or priority queue: • Implement stack using ...
Пікірлер: 277
Best explanation on KZread, many people simply showed the code instead of explaining the logic.
@techdose4u
3 жыл бұрын
Thanks man :)
@punnarahul90
2 жыл бұрын
Yes agreed neetcode and techdose are two beautiful channel
@sharpshooter5931
Жыл бұрын
Bro do u have code of this problem?? In C longuage
**Similar Questions, Monotonic Stack** 239. Sliding Window Maximum 496. Next Greater Element I 739. Daily Temperatures 862. Shortest Subarray with Sum at Least K 901. Online Stock Span 907. Sum of Subarray Minimums 687. Delivering Boxes from Storage to Ports
@tanyarajhans7533
3 жыл бұрын
Thank you!
@letsdoeverythinginoneweek9398
3 жыл бұрын
trapping rain water is mostly similar also
@contactdi8426
3 жыл бұрын
Thanks Buddy :)
@rohitshirur7640
2 жыл бұрын
thank you :)
@zaidshaikh2536
2 жыл бұрын
85. Maximal Rectangle
If I ever get selected in a Tier - 1 company. This channel would be having a big contribution to that.
@techdose4u
3 жыл бұрын
Means a lot ❤️ I wish you do
Best explanation on even why brute force works which no other channel was able to visually explain so well. Brilliant job keep doing more such videos for hard problems!
@techdose4u
3 жыл бұрын
Thanks :)
This is definitely the best explanation i could find on the internet. This guy showed how to solve it instead of just showing problem solution.
@techdose4u
3 жыл бұрын
😅
I think no one else can explain better than you. Problem was very overwhelming in the begining but at the end of 27th min I felt how patiently and clamly you made it clear with making my head hurt :) Keep making similar video. Really appreiciate your hard work
@techdose4u
3 жыл бұрын
Thanks :)
Of 4 videos I watched about this problem, this is the best. It describes the problem, builds up the required intuition and shows a real chain of thoughts that from the brute force approach leads to the optimal solution. Congratulation. Great job!!
@techdose4u
3 жыл бұрын
Thanks :)
It was hard to find video which explains the intuition behind the solution to this problem. And this channel always gets it right.Very good work. Btw, the max area calculation can be done when calculating the right limit array for each bar, so we need 2n traversals only.
@techdose4u
3 жыл бұрын
🤗 Right
I love how you walk through each and every step, which helps to sink in the algorithm and helps to clarify things I misunderstood or missed. Keep up with the good work!
The way u explain step by step and reaching from brute force to optimal is awesome !!✨ Thanks ♥️
@techdose4u
3 жыл бұрын
Welcome :)
I watched other channels for the same question but couldn't not understand clearly. But this is best explanation video for this question. Best part is the you start with brute force approach which is more important than directly moving to optimal one.
@techdose4u
2 жыл бұрын
😊
Your videos have helped me to a great extent. Recently I got placed at Barco systems as Software Engineer.
@techdose4u
3 жыл бұрын
Wow great ❤️ Congratulations 🎉
I just wanna thank you, because this is the best explanation on the planet for this problem.
I was struggling with this problem for a quite long time because I was not able to understand the intuition behind it. Thanks to techdose for clear explanation of the intuition
@techdose4u
3 жыл бұрын
👌🏼
Brilliant. Love the appoach of going from brute force to optimal. Love the step-by-step explanation and running through of the algorithm.
Saying from my down to heart it is the best explanation in youtube for this problem..Hope tech dose and Take you forward alone enough to getting placed in faang like companies..
Top notch explanation i just coded it myself after your explanation, keep doing the good work.
Life savour, for beginners like me. Thanks so much!
Phenomenal stuff. You can explain really well. Thanks.
best explanation with all given logic I did not understand this problem solution with paid course but Because of you sir I understood thank you so much
@techdose4u
3 жыл бұрын
Welcome 😊
I have tp say this is the best explanation among all the posts! Thank you so much for sharing!
@techdose4u
3 жыл бұрын
Welcome :)
Amazing, just amazing, after 4/5 videos, this is the only guy that makes sense!
@techdose4u
18 күн бұрын
thanks :)
Very well explanation , Thanks a lot it helped me a lot. Specially liked the fact that even the brute force was explained.
Thank you so much for such a clear explanation !
Thanks a lot sir 🙇🙏. Best vedio I have found on KZread on this topic. You explained each and every bit of problem clearly. Thanks again.
@techdose4u
3 жыл бұрын
Welcome
This is phenomenal. Couldn't find a better explanation than yours.
best explanation i can find on the internet for this question.
tanks a ton for such good explanation
Great explanation no one like u on KZread , you achieve one subscriber Great work man
So much clear explaination. Thank you very much :)
The best channel in KZread thankyou techdose
probably my first comment to a youtube video ! Keen and neat explanation for this problem .Thanks a lot
@techdose4u
3 жыл бұрын
Thanks :)
Thanks for detailed explanation. Keep up good work.
thank you soo much bro; you earned yourself a lifetime subscriber :)
Very helpful video!
Please what annotation tool do you use?
nice explanation brother...
very nice and swift explaination keep up the good work👍👍
Best explanation !
very good explanation
No one explains it better :) Thank you
@techdose4u
3 жыл бұрын
Welcome :)
thanks for the tutorial, so helpful!!
great explanation
How do you only have so little subscribers and views? You're doing an amazing job with these explanations
@techdose4u
2 жыл бұрын
Thanks 😊 Share in your contact 😜
This is the perfect explanation , I have ever heard of , Hats off ! sir
@techdose4u
2 жыл бұрын
Thanks 😊
the explanation is just the best!
great content
Thank you! I thought I could use a monatonic queue but still could not figure it out. You explained it very clearly
@techdose4u
2 жыл бұрын
Welcome 😀
Best Explainatin ever👍
best explanation on whole yt
clear explanation
Nice one
Thank you for explaining it very well. Can you tell if there is any dynamic programming involved here?
Good explanation
thank you it was clear
Thank U so much man, plz dont stop making videos if u ever think views are less
@techdose4u
3 жыл бұрын
Welcome :)
is there a soultion to this problem with single traversal ?
great explaination for every sticky points
@techdose4u
3 жыл бұрын
😊
Which tools do you use?
Superb explaination 👍
Clear thank You
much more complicated...but much more easy to understand.......thank you
@techdose4u
3 жыл бұрын
Welcome :)
Question: in above problem adding one more element a[6] = 0.75 , it will never be able to go to proper left limit as index 0 is popped out. Can you please explain ?
this channel is better than (All the combined paid edtech), thank you very much
@techdose4u
3 жыл бұрын
Welcome :)
Sooooo helpful, you have my THANKS!
@techdose4u
3 жыл бұрын
Welcome :)
Thanks a lot for our videos
The best explanation on the internet.
@techdose4u
3 жыл бұрын
Thanks :)
Keep it up
Best Explanation ever on the YourTube.
@techdose4u
3 жыл бұрын
Thanks :)
People are showing steps of algorithm without explaining anything.... This is a gem of video which clearly explains the 'why' of algorithm. great job. btw, I see another solution in Leetcode with one loop, is it similar to this one? I dont understand that one loop solution.
@techdose4u
2 жыл бұрын
No need to overkill a problem if you can solve in same time complexity :)
@dataman4503
2 жыл бұрын
@@techdose4u I understand that the time complexity is O(n),but during interviews some idiot interviewers will ask a follow-up question like 'hey can you do this without traversing the loop twice?' 😂
You saved my life with this video, thank you very much :D
@techdose4u
2 жыл бұрын
Great ❤️
Good video
Thank you so much! Really helpful explanation :)
@techdose4u
2 жыл бұрын
Welcome :)
won't popping the stack with a while loop increase the complexity to something greater than O(N)?
Brilliant! Great Explanation.
@techdose4u
3 жыл бұрын
Thanks :)
I have watched many videos on this problem but couldn't understand anything . Best explanation on youtube !
@techdose4u
3 жыл бұрын
Thanks :)
your explanations are better than 20k rs paid courses' teachers.Thanks a lot
But popping from stack might need O(n) and traversal from left to right and right to left requires O(n), hence the total complexity should be O(n^2), right ?
How is this O(N) in the solution with the stack, when we're foreach-ing over all bars (O(N)) but then _internally_ also while-ing through the existing stack and popping (times another O(n))? Isn't this a straight O(N^2). They are clear nested loops to me.
@samr6781
2 жыл бұрын
Nested loops don't mean O(N^2). How many times the stack will be filled and popped? 2N at most! So, 2N for the inner while loop + N for the outer loop = 3N = O(N) time complexity.
Nice :)
An Intuitive explanation. Thanks.!
@techdose4u
3 жыл бұрын
Welcome :)
Great explanation sir.... the ways you explain is pretty awesome... very helpful.. thank you for such a great tutorial
@techdose4u
2 жыл бұрын
Welcome 😀
Awesome explaination🤗🤗 Bhaiya
@techdose4u
3 жыл бұрын
Thanks
Best Explanation !!
@techdose4u
Жыл бұрын
Thanks:)
We can reduce on more traversal by doing max area in the second one itself
Can you please tell how to intuitively prove the time complexity to be O(n) for this problem? Does pushing and popping not contribute to greater time complexity?
@ferozmohammad5841
2 жыл бұрын
pushing and popping in stacks takes time complexity O(1). And as we're traversing through the given array once, the complexity became O(n).
@DenisIsidoro
2 жыл бұрын
@@ferozmohammad5841 In order to build the stack, there's a "while" inside a "for". Shouldn't it be O(n^2)?
@DenisIsidoro
2 жыл бұрын
The worst-case scenario happens when the bars are strictly decreasing and it's O(n^2) IIUC
Perfect explaination . Keep goinggggg
@techdose4u
3 жыл бұрын
Thanks
Grt explanation
@techdose4u
3 жыл бұрын
Thanks :)
Thanks
thanks a lot sir for such a amazing explanation
@techdose4u
3 жыл бұрын
Welcome
Amazing Explaination
@techdose4u
2 жыл бұрын
Thanks 😊
27:05 which c++ version is this ??? , supports " and " keyword 🤔
@techdose4u
3 жыл бұрын
Dunno the version.
thanks man")
Superb explanation!! Cheers man!
@techdose4u
3 жыл бұрын
Thanks
Bro plz include manachers algorithm I have been suggesting tech dose to all of my friends juat bcz of the great content ... Good job
@techdose4u
3 жыл бұрын
It will come someday :)
Finally in this channel i understood the solution. what about space complexity? i assume it's O(N). correct me if i am wrong.
I was asked this question in my interview of meditab ahmedabad company.
But we are still using while loop inside for loop....so how can the complexity be O(n)
Great explanation. Thanks a lot.
@techdose4u
3 жыл бұрын
Welcome
Thank you so much sir really helpful
@techdose4u
3 жыл бұрын
Welcome