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

  • @venuvenu2719
    @venuvenu27193 жыл бұрын

    Best explanation on KZread, 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
    @devplus71313 жыл бұрын

    **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
    @MadForCs163 жыл бұрын

    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
    @jrajesh113 жыл бұрын

    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
    @pruthviraj80713 жыл бұрын

    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 жыл бұрын

    😅

  • @KushalBhatia
    @KushalBhatia3 жыл бұрын

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

  • @alessandrocamillo4939
    @alessandrocamillo49393 жыл бұрын

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

  • @ayushupadhyaya7656
    @ayushupadhyaya76563 жыл бұрын

    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
    @hakoHiyo2713 жыл бұрын

    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
    @snehabaser31553 жыл бұрын

    The way u explain step by step and reaching from brute force to optimal is awesome !!✨ Thanks ♥️

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @DevMaster947
    @DevMaster9472 жыл бұрын

    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-ue8uj3 жыл бұрын

    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
    @mohamedantar12493 ай бұрын

    I just wanna thank you, because this is the best explanation on the planet for this problem.

  • @dayanandraut5660
    @dayanandraut56603 жыл бұрын

    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 жыл бұрын

    👌🏼

  • @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.

  • @gurubalaji5611
    @gurubalaji56113 жыл бұрын

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

  • @SairamDasari2000
    @SairamDasari20003 ай бұрын

    Top notch explanation i just coded it myself after your explanation, keep doing the good work.

  • @pratikjain3323
    @pratikjain33233 жыл бұрын

    Life savour, for beginners like me. Thanks so much!

  • @mazharuddin3647
    @mazharuddin36475 ай бұрын

    Phenomenal stuff. You can explain really well. Thanks.

  • @Ajaykumar-pv5lp
    @Ajaykumar-pv5lp3 жыл бұрын

    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 😊

  • @KChen-ks4le
    @KChen-ks4le3 жыл бұрын

    I have tp say this is the best explanation among all the posts! Thank you so much for sharing!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @tanzimchowdhury320
    @tanzimchowdhury32019 күн бұрын

    Amazing, just amazing, after 4/5 videos, this is the only guy that makes sense!

  • @techdose4u

    @techdose4u

    18 күн бұрын

    thanks :)

  • @sakibshahon
    @sakibshahon3 жыл бұрын

    Very well explanation , Thanks a lot it helped me a lot. Specially liked the fact that even the brute force was explained.

  • @vishalcelestine8654
    @vishalcelestine86543 жыл бұрын

    Thank you so much for such a clear explanation !

  • @krishnananddubey2870
    @krishnananddubey28703 жыл бұрын

    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

    @techdose4u

    3 жыл бұрын

    Welcome

  • @harpercfc_
    @harpercfc_2 жыл бұрын

    This is phenomenal. Couldn't find a better explanation than yours.

  • @aasthagautam9480
    @aasthagautam94802 жыл бұрын

    best explanation i can find on the internet for this question.

  • @rejetimeghavardhan7805
    @rejetimeghavardhan78052 жыл бұрын

    tanks a ton for such good explanation

  • @jatinmittal9184
    @jatinmittal91843 жыл бұрын

    Great explanation no one like u on KZread , you achieve one subscriber Great work man

  • @borhansgallery5190
    @borhansgallery5190 Жыл бұрын

    So much clear explaination. Thank you very much :)

  • @pamupravallika2391
    @pamupravallika23915 ай бұрын

    The best channel in KZread thankyou techdose

  • @naveenmurali3972
    @naveenmurali39723 жыл бұрын

    probably my first comment to a youtube video ! Keen and neat explanation for this problem .Thanks a lot

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @sudhanshuverma1987
    @sudhanshuverma19872 жыл бұрын

    Thanks for detailed explanation. Keep up good work.

  • @chaitanya14499
    @chaitanya144992 жыл бұрын

    thank you soo much bro; you earned yourself a lifetime subscriber :)

  • @callmedanioo
    @callmedanioo2 жыл бұрын

    Very helpful video!

  • @calebo.a.6270
    @calebo.a.62702 жыл бұрын

    Please what annotation tool do you use?

  • @NiteshSingh5375
    @NiteshSingh53753 жыл бұрын

    nice explanation brother...

  • @suryansh70
    @suryansh702 жыл бұрын

    very nice and swift explaination keep up the good work👍👍

  • @ronit-22
    @ronit-22 Жыл бұрын

    Best explanation !

  • @23butlouder47
    @23butlouder472 жыл бұрын

    very good explanation

  • @__abhiruchigupta__
    @__abhiruchigupta__3 жыл бұрын

    No one explains it better :) Thank you

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @ferozmohammad5841
    @ferozmohammad58412 жыл бұрын

    thanks for the tutorial, so helpful!!

  • @balajijangde8470
    @balajijangde84703 жыл бұрын

    great explanation

  • @JC-hj3sn
    @JC-hj3sn2 жыл бұрын

    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 😜

  • @saicharan4669
    @saicharan46692 жыл бұрын

    This is the perfect explanation , I have ever heard of , Hats off ! sir

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Thanks 😊

  • @adityaagarwal7291
    @adityaagarwal72913 жыл бұрын

    the explanation is just the best!

  • @Ravin_raze
    @Ravin_raze3 жыл бұрын

    great content

  • @zehuazhou3390
    @zehuazhou33902 жыл бұрын

    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 😀

  • @anuragkhard
    @anuragkhard Жыл бұрын

    Best Explainatin ever👍

  • @bepracticalweb
    @bepracticalweb Жыл бұрын

    best explanation on whole yt

  • @TheKeyToMusicOfficial
    @TheKeyToMusicOfficial2 жыл бұрын

    clear explanation

  • @satyasuvartha8595
    @satyasuvartha8595 Жыл бұрын

    Nice one

  • @jansiranis4480
    @jansiranis44802 жыл бұрын

    Thank you for explaining it very well. Can you tell if there is any dynamic programming involved here?

  • @krishnaprabeesh2415
    @krishnaprabeesh24152 жыл бұрын

    Good explanation

  • @venkataraman7962
    @venkataraman79622 жыл бұрын

    thank you it was clear

  • @ADNANAHMED-eo5xx
    @ADNANAHMED-eo5xx3 жыл бұрын

    Thank U so much man, plz dont stop making videos if u ever think views are less

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @evilgamer578
    @evilgamer5782 жыл бұрын

    is there a soultion to this problem with single traversal ?

  • @rishabhkumar8115
    @rishabhkumar81153 жыл бұрын

    great explaination for every sticky points

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    😊

  • @santoshreddy3
    @santoshreddy33 жыл бұрын

    Which tools do you use?

  • @meditationmusic9997
    @meditationmusic99972 жыл бұрын

    Superb explaination 👍

  • @kolhesatish
    @kolhesatish Жыл бұрын

    Clear thank You

  • @ankitranjan88
    @ankitranjan883 жыл бұрын

    much more complicated...but much more easy to understand.......thank you

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @Push1781
    @Push17812 жыл бұрын

    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 ?

  • @Suraj6642
    @Suraj66423 жыл бұрын

    this channel is better than (All the combined paid edtech), thank you very much

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @jigtbbbf
    @jigtbbbf3 жыл бұрын

    Sooooo helpful, you have my THANKS!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @Otnielush
    @Otnielush2 жыл бұрын

    Thanks a lot for our videos

  • @Aryan-fi2qf
    @Aryan-fi2qf3 жыл бұрын

    The best explanation on the internet.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @meditationmusic9997
    @meditationmusic99972 жыл бұрын

    Keep it up

  • @AbhishekThakur-gz6ul
    @AbhishekThakur-gz6ul3 жыл бұрын

    Best Explanation ever on the YourTube.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @dataman4503
    @dataman45032 жыл бұрын

    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?' 😂

  • @legitarrra3845
    @legitarrra38452 жыл бұрын

    You saved my life with this video, thank you very much :D

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Great ❤️

  • @fax160
    @fax160Ай бұрын

    Good video

  • @elliotho3015
    @elliotho30152 жыл бұрын

    Thank you so much! Really helpful explanation :)

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Welcome :)

  • @Aayush28jun
    @Aayush28jun Жыл бұрын

    won't popping the stack with a while loop increase the complexity to something greater than O(N)?

  • @Arjun69
    @Arjun693 жыл бұрын

    Brilliant! Great Explanation.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @bvsrevanth6856
    @bvsrevanth68563 жыл бұрын

    I have watched many videos on this problem but couldn't understand anything . Best explanation on youtube !

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @avikmallick2493
    @avikmallick24933 жыл бұрын

    your explanations are better than 20k rs paid courses' teachers.Thanks a lot

  • @akashkp9013
    @akashkp90133 ай бұрын

    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 ?

  • @stepanstulov9871
    @stepanstulov98712 жыл бұрын

    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.

  • @kongzilla2897
    @kongzilla28973 жыл бұрын

    Nice :)

  • @sulaimant5690
    @sulaimant56903 жыл бұрын

    An Intuitive explanation. Thanks.!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @pujabhardwaj7569
    @pujabhardwaj75692 жыл бұрын

    Great explanation sir.... the ways you explain is pretty awesome... very helpful.. thank you for such a great tutorial

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Welcome 😀

  • @shrutigupta9319
    @shrutigupta93193 жыл бұрын

    Awesome explaination🤗🤗 Bhaiya

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @vivekswami2182
    @vivekswami2182 Жыл бұрын

    Best Explanation !!

  • @techdose4u

    @techdose4u

    Жыл бұрын

    Thanks:)

  • @animalbubble5937
    @animalbubble59373 жыл бұрын

    We can reduce on more traversal by doing max area in the second one itself

  • @madmax2442
    @madmax24423 жыл бұрын

    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

  • @thangnguyen8063
    @thangnguyen80633 жыл бұрын

    Perfect explaination . Keep goinggggg

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @laraibanwar1618
    @laraibanwar16183 жыл бұрын

    Grt explanation

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @shivanshbansal3212
    @shivanshbansal32122 жыл бұрын

    Thanks

  • @techykush7192
    @techykush71923 жыл бұрын

    thanks a lot sir for such a amazing explanation

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome

  • @animeshjain4594
    @animeshjain45942 жыл бұрын

    Amazing Explaination

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Thanks 😊

  • @bhupeshpattanaik7150
    @bhupeshpattanaik71503 жыл бұрын

    27:05 which c++ version is this ??? , supports " and " keyword 🤔

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Dunno the version.

  • @Unknown-ck7fi
    @Unknown-ck7fi2 жыл бұрын

    thanks man")

  • @satwikkhandelwal3722
    @satwikkhandelwal37223 жыл бұрын

    Superb explanation!! Cheers man!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @laraibanwar1618
    @laraibanwar16183 жыл бұрын

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

  • @jayeshbhoi6844
    @jayeshbhoi68442 жыл бұрын

    Finally in this channel i understood the solution. what about space complexity? i assume it's O(N). correct me if i am wrong.

  • @bhargav1811
    @bhargav18114 ай бұрын

    I was asked this question in my interview of meditab ahmedabad company.

  • @mohammednihad9548
    @mohammednihad95483 жыл бұрын

    But we are still using while loop inside for loop....so how can the complexity be O(n)

  • @adilsyed8369
    @adilsyed83693 жыл бұрын

    Great explanation. Thanks a lot.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome

  • @ShivamGupta-cx3hy
    @ShivamGupta-cx3hy3 жыл бұрын

    Thank you so much sir really helpful

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome