Largest rectangle in Histogram | Leetcode #84

  Рет қаралды 109,317

Techdose

Techdose

3 жыл бұрын

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
@venuvenu2719
@venuvenu2719 3 жыл бұрын
Best explanation on KZbin, many people simply showed the code instead of explaining the logic.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks man :)
@punnarahul90
@punnarahul90 2 жыл бұрын
Yes agreed neetcode and techdose are two beautiful channel
@sharpshooter5931
@sharpshooter5931 Жыл бұрын
Bro do u have code of this problem?? In C longuage
@devplus7131
@devplus7131 3 жыл бұрын
**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
@tanyarajhans7533 3 жыл бұрын
Thank you!
@letsdoeverythinginoneweek9398
@letsdoeverythinginoneweek9398 3 жыл бұрын
trapping rain water is mostly similar also
@contactdi8426
@contactdi8426 3 жыл бұрын
Thanks Buddy :)
@rohitshirur7640
@rohitshirur7640 2 жыл бұрын
thank you :)
@zaidshaikh2536
@zaidshaikh2536 2 жыл бұрын
85. Maximal Rectangle
@MadForCs16
@MadForCs16 3 жыл бұрын
If I ever get selected in a Tier - 1 company. This channel would be having a big contribution to that.
@techdose4u
@techdose4u 3 жыл бұрын
Means a lot ❤️ I wish you do
@jrajesh11
@jrajesh11 3 жыл бұрын
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
@techdose4u 3 жыл бұрын
Thanks :)
@pruthviraj8071
@pruthviraj8071 3 жыл бұрын
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
@techdose4u 3 жыл бұрын
😅
@alessandrocamillo4939
@alessandrocamillo4939 3 жыл бұрын
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
@techdose4u 3 жыл бұрын
Thanks :)
@KushalBhatia
@KushalBhatia 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
@techdose4u 3 жыл бұрын
Thanks :)
@ayushupadhyaya7656
@ayushupadhyaya7656 3 жыл бұрын
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
@techdose4u 3 жыл бұрын
🤗 Right
@hakoHiyo271
@hakoHiyo271 3 жыл бұрын
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!
@snehabaser3155
@snehabaser3155 3 жыл бұрын
The way u explain step by step and reaching from brute force to optimal is awesome !!✨ Thanks ♥️
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@DevMaster947
@DevMaster947 2 жыл бұрын
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
@techdose4u 2 жыл бұрын
😊
@souravkumar-ue8uj
@souravkumar-ue8uj 3 жыл бұрын
Your videos have helped me to a great extent. Recently I got placed at Barco systems as Software Engineer.
@techdose4u
@techdose4u 3 жыл бұрын
Wow great ❤️ Congratulations 🎉
@mohamedantar1249
@mohamedantar1249 4 ай бұрын
I just wanna thank you, because this is the best explanation on the planet for this problem.
@danny65769
@danny65769 Жыл бұрын
Brilliant. Love the appoach of going from brute force to optimal. Love the step-by-step explanation and running through of the algorithm.
@SairamDasari2000
@SairamDasari2000 3 ай бұрын
Top notch explanation i just coded it myself after your explanation, keep doing the good work.
@gurubalaji5611
@gurubalaji5611 3 жыл бұрын
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..
@dayanandraut5660
@dayanandraut5660 3 жыл бұрын
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
@techdose4u 3 жыл бұрын
👌🏼
@pratikjain3323
@pratikjain3323 3 жыл бұрын
Life savour, for beginners like me. Thanks so much!
@Ajaykumar-pv5lp
@Ajaykumar-pv5lp 3 жыл бұрын
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
@techdose4u 3 жыл бұрын
Welcome 😊
@mazharuddin3647
@mazharuddin3647 5 ай бұрын
Phenomenal stuff. You can explain really well. Thanks.
@KChen-ks4le
@KChen-ks4le 3 жыл бұрын
I have tp say this is the best explanation among all the posts! Thank you so much for sharing!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@jatinmittal9184
@jatinmittal9184 3 жыл бұрын
Great explanation no one like u on KZbin , you achieve one subscriber Great work man
@naveenmurali3972
@naveenmurali3972 3 жыл бұрын
probably my first comment to a youtube video ! Keen and neat explanation for this problem .Thanks a lot
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@sakibshahon
@sakibshahon 3 жыл бұрын
Very well explanation , Thanks a lot it helped me a lot. Specially liked the fact that even the brute force was explained.
@krishnananddubey2870
@krishnananddubey2870 3 жыл бұрын
Thanks a lot sir 🙇🙏. Best vedio I have found on KZbin on this topic. You explained each and every bit of problem clearly. Thanks again.
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@harpercfc_
@harpercfc_ 2 жыл бұрын
This is phenomenal. Couldn't find a better explanation than yours.
@aasthagautam9480
@aasthagautam9480 2 жыл бұрын
best explanation i can find on the internet for this question.
@borhansgallery5190
@borhansgallery5190 Жыл бұрын
So much clear explaination. Thank you very much :)
@tanzimchowdhury320
@tanzimchowdhury320 Ай бұрын
Amazing, just amazing, after 4/5 videos, this is the only guy that makes sense!
@techdose4u
@techdose4u Ай бұрын
thanks :)
@vishalcelestine8654
@vishalcelestine8654 3 жыл бұрын
Thank you so much for such a clear explanation !
@JC-hj3sn
@JC-hj3sn 2 жыл бұрын
How do you only have so little subscribers and views? You're doing an amazing job with these explanations
@techdose4u
@techdose4u 2 жыл бұрын
Thanks 😊 Share in your contact 😜
@pamupravallika2391
@pamupravallika2391 6 ай бұрын
The best channel in KZbin thankyou techdose
@sudhanshuverma1987
@sudhanshuverma1987 2 жыл бұрын
Thanks for detailed explanation. Keep up good work.
@chaitanya14499
@chaitanya14499 2 жыл бұрын
thank you soo much bro; you earned yourself a lifetime subscriber :)
@suryansh70
@suryansh70 2 жыл бұрын
very nice and swift explaination keep up the good work👍👍
@saicharan4669
@saicharan4669 2 жыл бұрын
This is the perfect explanation , I have ever heard of , Hats off ! sir
@techdose4u
@techdose4u 2 жыл бұрын
Thanks 😊
@rejetimeghavardhan7805
@rejetimeghavardhan7805 2 жыл бұрын
tanks a ton for such good explanation
@adityaagarwal7291
@adityaagarwal7291 3 жыл бұрын
the explanation is just the best!
@bepracticalweb
@bepracticalweb Жыл бұрын
best explanation on whole yt
@callmedanioo
@callmedanioo 2 жыл бұрын
Very helpful video!
@helpingUgrow
@helpingUgrow 3 жыл бұрын
No one explains it better :) Thank you
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@ronit-22
@ronit-22 Жыл бұрын
Best explanation !
@ADNANAHMED-eo5xx
@ADNANAHMED-eo5xx 3 жыл бұрын
Thank U so much man, plz dont stop making videos if u ever think views are less
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@anuragkhard
@anuragkhard Жыл бұрын
Best Explainatin ever👍
@meditationmusic9997
@meditationmusic9997 2 жыл бұрын
Superb explaination 👍
@ferozmohammad5841
@ferozmohammad5841 2 жыл бұрын
thanks for the tutorial, so helpful!!
@Suraj6642
@Suraj6642 3 жыл бұрын
this channel is better than (All the combined paid edtech), thank you very much
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@avikmallick2493
@avikmallick2493 3 жыл бұрын
your explanations are better than 20k rs paid courses' teachers.Thanks a lot
@23butlouder47
@23butlouder47 2 жыл бұрын
very good explanation
@NiteshSingh5375
@NiteshSingh5375 3 жыл бұрын
nice explanation brother...
@akashkp9013
@akashkp9013 3 ай бұрын
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 ?
@balajijangde8470
@balajijangde8470 3 жыл бұрын
great explanation
@jansiranis4480
@jansiranis4480 2 жыл бұрын
Thank you for explaining it very well. Can you tell if there is any dynamic programming involved here?
@legitarrra3845
@legitarrra3845 2 жыл бұрын
You saved my life with this video, thank you very much :D
@techdose4u
@techdose4u 2 жыл бұрын
Great ❤️
@AbhishekThakur-gz6ul
@AbhishekThakur-gz6ul 3 жыл бұрын
Best Explanation ever on the YourTube.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@dataman4503
@dataman4503 2 жыл бұрын
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
@techdose4u 2 жыл бұрын
No need to overkill a problem if you can solve in same time complexity :)
@dataman4503
@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?' 😂
@pujabhardwaj7569
@pujabhardwaj7569 2 жыл бұрын
Great explanation sir.... the ways you explain is pretty awesome... very helpful.. thank you for such a great tutorial
@techdose4u
@techdose4u 2 жыл бұрын
Welcome 😀
@Arjun69
@Arjun69 3 жыл бұрын
Brilliant! Great Explanation.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@Aryan-fi2qf
@Aryan-fi2qf 3 жыл бұрын
The best explanation on the internet.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@rishabhkumar8115
@rishabhkumar8115 3 жыл бұрын
great explaination for every sticky points
@techdose4u
@techdose4u 3 жыл бұрын
😊
@ankitranjan88
@ankitranjan88 3 жыл бұрын
much more complicated...but much more easy to understand.......thank you
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@elliotho3015
@elliotho3015 2 жыл бұрын
Thank you so much! Really helpful explanation :)
@techdose4u
@techdose4u 2 жыл бұрын
Welcome :)
@Ravin_raze
@Ravin_raze 3 жыл бұрын
great content
@Push1781
@Push1781 2 жыл бұрын
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 ?
@krishnaprabeesh2415
@krishnaprabeesh2415 2 жыл бұрын
Good explanation
@venkataraman7962
@venkataraman7962 2 жыл бұрын
thank you it was clear
@zehuazhou3390
@zehuazhou3390 2 жыл бұрын
Thank you! I thought I could use a monatonic queue but still could not figure it out. You explained it very clearly
@techdose4u
@techdose4u 2 жыл бұрын
Welcome 😀
@satyasuvartha8595
@satyasuvartha8595 Жыл бұрын
Nice one
@jigtbbbf
@jigtbbbf 3 жыл бұрын
Sooooo helpful, you have my THANKS!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@TheKeyToMusicOfficial
@TheKeyToMusicOfficial 2 жыл бұрын
clear explanation
@bvsrevanth6856
@bvsrevanth6856 3 жыл бұрын
I have watched many videos on this problem but couldn't understand anything . Best explanation on youtube !
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@Aayush28jun
@Aayush28jun 2 жыл бұрын
won't popping the stack with a while loop increase the complexity to something greater than O(N)?
@kolhesatish
@kolhesatish Жыл бұрын
Clear thank You
@sulaimant5690
@sulaimant5690 3 жыл бұрын
An Intuitive explanation. Thanks.!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@vivekswami2182
@vivekswami2182 Жыл бұрын
Best Explanation !!
@techdose4u
@techdose4u Жыл бұрын
Thanks:)
@madmax2442
@madmax2442 3 жыл бұрын
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
@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
@DenisIsidoro 2 жыл бұрын
@@ferozmohammad5841 In order to build the stack, there's a "while" inside a "for". Shouldn't it be O(n^2)?
@DenisIsidoro
@DenisIsidoro 2 жыл бұрын
The worst-case scenario happens when the bars are strictly decreasing and it's O(n^2) IIUC
@jayeshbhoi6844
@jayeshbhoi6844 2 жыл бұрын
Finally in this channel i understood the solution. what about space complexity? i assume it's O(N). correct me if i am wrong.
@thangnguyen8063
@thangnguyen8063 3 жыл бұрын
Perfect explaination . Keep goinggggg
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@fax160
@fax160 2 ай бұрын
Good video
@satwikkhandelwal3722
@satwikkhandelwal3722 3 жыл бұрын
Superb explanation!! Cheers man!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@santoshreddy3
@santoshreddy3 3 жыл бұрын
Which tools do you use?
@Otnielush
@Otnielush 2 жыл бұрын
Thanks a lot for our videos
@calebo.a.6270
@calebo.a.6270 2 жыл бұрын
Please what annotation tool do you use?
@techykush7192
@techykush7192 3 жыл бұрын
thanks a lot sir for such a amazing explanation
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@animeshjain4594
@animeshjain4594 2 жыл бұрын
Amazing Explaination
@techdose4u
@techdose4u 2 жыл бұрын
Thanks 😊
@stepanstulov9871
@stepanstulov9871 2 жыл бұрын
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
@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.
@laraibanwar1618
@laraibanwar1618 3 жыл бұрын
Grt explanation
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@shrutigupta9319
@shrutigupta9319 3 жыл бұрын
Awesome explaination🤗🤗 Bhaiya
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@laraibanwar1618
@laraibanwar1618 3 жыл бұрын
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
@techdose4u 3 жыл бұрын
It will come someday :)
@adilsyed8369
@adilsyed8369 3 жыл бұрын
Great explanation. Thanks a lot.
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@SANDIPKUMAR-es7qh
@SANDIPKUMAR-es7qh 2 жыл бұрын
This is very nice video and explained the problem in more simply way. int hist[] = {1,4,9,5,7,2,10,3,5}; OutPut : 16 I tried to run this program using the same logic for the above array. But not able to understand why it's giving 16 as an output. I think it should be 10*3=30. Correct me if my understanding is wrong
@shlomokon
@shlomokon 8 ай бұрын
Great explanation but some of the slides have some pre-existing values which don't fit the current example. The presenter strikes out those unmatching values and writes the correct ones next to them. It would be easier to follow if there were no such pre-existing values from a different example.
@bhargav1811
@bhargav1811 4 ай бұрын
I was asked this question in my interview of meditab ahmedabad company.
@evilgamer578
@evilgamer578 3 жыл бұрын
is there a soultion to this problem with single traversal ?
@biswajeetpadhi5713
@biswajeetpadhi5713 3 жыл бұрын
Awsome explanation sir..
@techdose4u
@techdose4u 3 жыл бұрын
Thanks ☺️
@theghostwhowalk
@theghostwhowalk 3 жыл бұрын
Amazing explanation!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks Aditya :)
@AshishKumar-qk9rc
@AshishKumar-qk9rc 3 жыл бұрын
It was so good.... Amazing man
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@ShivamGupta-cx3hy
@ShivamGupta-cx3hy 3 жыл бұрын
Thank you so much sir really helpful
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@animalbubble5937
@animalbubble5937 3 жыл бұрын
We can reduce on more traversal by doing max area in the second one itself
@istvanszabo6875
@istvanszabo6875 Жыл бұрын
great job!
@techdose4u
@techdose4u Жыл бұрын
Thanks
Trapping Rainwater Problem | Leetcode #42
34:12
Techdose
Рет қаралды 96 М.
Can A Seed Grow In Your Nose? 🤔
00:33
Zack D. Films
Рет қаралды 29 МЛН
小蚂蚁被感动了!火影忍者 #佐助 #家庭
00:54
火影忍者一家
Рет қаралды 32 МЛН
No empty
00:35
Mamasoboliha
Рет қаралды 10 МЛН
LARGEST RECTANGLE IN HISTOGRAM - Leetcode 84 - Python
17:20
NeetCode
Рет қаралды 208 М.
Coding Interview Problem: Largest Rectangle in a Histogram
16:18
Jackson Gabbard
Рет қаралды 307 М.
Largest Rectangle in Histogram | Part - 1 | Leetcode 84
30:00
take U forward
Рет қаралды 212 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Word Ladder | Leetcode #127
19:19
Techdose
Рет қаралды 70 М.
Maximal square | Dynamic programming | Leetcode #221
18:27
Techdose
Рет қаралды 70 М.
5 Math Skills Every Programmer Needs
9:08
Sahil & Sarra
Рет қаралды 1 МЛН