Largest Area under Histogram efficient algorithm using Stack | Coding Interview Question

Поділитися
Вставка
  • Опубліковано 5 жов 2024
  • How to find out the largest area under Histogram in a Stack ? Learn to solve in this tutorial :)
    For more online courses visit - www.codingblock...
    Check courses on - online.codingbl... [Free Trial Available]
    Coding Blocks is pleased to announce courses like C++ and Java, Data Structures and Algorithms, Web and Android Development(Java and Kotlin), Competitive Programming, Coding Interview Preparation and Machine Learning, AI and more.
    #CodingBlocks #ProgrammingMadeEasy #LearnCodingOnline
    Like our FaceBook Page - / codingblocksindia
    Follow us on Instagram - / codingblocks
    Follow us on Twitter - / codingblocksin
    Source code available on -github.com/cod...
    For more interesting tutorials - / @codingblocksindia

КОМЕНТАРІ • 110

  • @noone_and_nobody
    @noone_and_nobody 4 роки тому +9

    I went through atleast a few videos before giving this one a try and boy, it was completely worth it. The explanation with a running example, just superb. Kudos to the team!

  • @NareshKumar-dw9xp
    @NareshKumar-dw9xp 4 роки тому +16

    This is the best explanation I have ever seen about any question. Everyone is running behind codes but no one is talking about mathematics behind that. I have seen a lot of videos related to this but did not satisfy. But now I am fully confident in these problems. Thank you so much , you always use to explain hard problems in quite easy manner .

  • @cyanzhoufeng
    @cyanzhoufeng 4 роки тому

    I don't know why so many ppl downvote this excellent presentation , this is best explanation I found, rather than other videos which just show the formula and conclusion only. Thanks for the detailed illustration

  • @baurks
    @baurks 5 років тому +35

    The best explanation so far. I am still lagging behind on the intuition, but now I can prove the algorithm by induction.

  • @JuanSB827
    @JuanSB827 5 років тому +10

    Finally an explanation that makes sense!

  • @tusharsinha1772
    @tusharsinha1772 4 роки тому +1

    The only explanation on youtube for this problem which makes sense.

  • @vanshthukral5477
    @vanshthukral5477 5 років тому +8

    Expressing my gratitude to everyone who made creation of this video possible.A really simple ,thorough and good explanation.

  • @mello.maniac
    @mello.maniac 6 років тому +7

    the approach was nice, I liked the explanation with a proper example, Just one suggestion: please remove double logos from the bottom right corner.

  • @srinivaspavan9119
    @srinivaspavan9119 4 роки тому +5

    Dude, you saved my day. all the remaining videos were a waste of time. 🤔 I wonder how the hell did the first person who solved this question in the world got this idea or thought of this algorithm. It's just amazing 🤯

    • @aasdffew
      @aasdffew 4 роки тому

      He must have been a genius...

    • @saurabhverma2729
      @saurabhverma2729 3 роки тому

      I feel the same for the person who designed this problem.

  • @sachingoyal6197
    @sachingoyal6197 6 років тому +16

    Finally got it after going through so many videos.

    • @venuvenu2719
      @venuvenu2719 5 років тому +4

      I still have no idea how someone gets idea to use stack for this problem

    • @harshitsaini15
      @harshitsaini15 4 роки тому

      @@venuvenu2719 Yes, I'm thinking the same :(

  • @AmanKumar-hr5dc
    @AmanKumar-hr5dc 4 роки тому +1

    Very nice explanation
    Now i got the intution behind this!

  • @kvishalreddy_3742
    @kvishalreddy_3742 3 роки тому +1

    best explanation thanks

  • @ananyasharma101
    @ananyasharma101 4 роки тому

    Best solution to this problem among all videos

  • @aasdffew
    @aasdffew 4 роки тому +1

    Now I am able to understand the logic, thanks

  • @ShaliniNegi24
    @ShaliniNegi24 4 роки тому +2

    Honestly very nice explanation.

  • @swapnikapandey6882
    @swapnikapandey6882 3 роки тому

    easiest explanation so far.Keep making such videos.

  • @israelmcculley2927
    @israelmcculley2927 3 роки тому

    I found your explanation confusing because the first step you explained was that you would only push an index onto the stack if the height is larger than the previous height, but then you went on to push literally every single element into the stack regardless of its height comparison with the previous height.

  • @anirudhatalmale5575
    @anirudhatalmale5575 4 роки тому

    The best and simplest explanation.

  • @shagun9155
    @shagun9155 3 роки тому

    Woow what a worthy explanation:-) only now I am able to get the intuition of this problem

  • @rajugaurav5052
    @rajugaurav5052 3 роки тому

    Thank you so much , finally I understand the intuition behind this algorithm :))

  • @sprajapati2011
    @sprajapati2011 5 років тому +1

    You just ammmazingly explained.

  • @katyayanikrishnan8725
    @katyayanikrishnan8725 4 роки тому

    best video so far on this topic.

  • @nayankhuman1043
    @nayankhuman1043 3 роки тому

    best explanation ever

  • @mohammedsuhailbasha4860
    @mohammedsuhailbasha4860 5 років тому +6

    Very nice lecture...... always upload this type of lectures bro.this will more helpful to us

    • @CodingBlocksIndia
      @CodingBlocksIndia  5 років тому +1

      Check out complete course at online.codingblocks.com

  • @shivampawar5074
    @shivampawar5074 4 роки тому +1

    But what if the numbers are in increasing order e.g. 1,2,3,4,5 ?

  • @WimpyWarlord
    @WimpyWarlord 5 років тому +4

    Set the speed to 1.25 or 1.5 and thank me later.
    The video was a lot better than others made on similar topic , yet you would have to develop the intuition on your own .

  • @nafishaftab9323
    @nafishaftab9323 5 років тому +4

    Best explanation.....even better than geeksforgeeks

  • @ajitdavis5100
    @ajitdavis5100 4 роки тому

    very well explained
    thank you so much

  • @participiopassado
    @participiopassado 4 роки тому +4

    Thank you very much for the explanation. Now I finally understood this question! Subscribed!

  • @ujjvalsharma5055
    @ujjvalsharma5055 4 роки тому

    I really appreciate what you did here. Amazing work and such explanation was missing in others. I encourage to put out more video and keep up the good work.

  • @vartikavarshney8303
    @vartikavarshney8303 5 років тому +4

    its long but definitely worth it

  • @punitpawar4231
    @punitpawar4231 4 роки тому

    Thanks for this video. Nice Explanation. Where is the video or the link to the code ?

  • @reassume4826
    @reassume4826 4 роки тому

    Thank You Coding Blocks.

  • @sukritkapil2562
    @sukritkapil2562 4 роки тому

    Nice explanation. Thanks!

  • @rishulmatta16
    @rishulmatta16 6 років тому

    Amazing explanation loved it! Scroll to 13:41

  • @mayurkoli4145
    @mayurkoli4145 4 роки тому +3

    what if bar's are in increasing order eg -> 1, 2, 4, 5, 6, 7

    • @zod1ack_csgo_clips425
      @zod1ack_csgo_clips425 4 роки тому

      It will be the worst case scenario for this algo with O(2n) complexity.

    • @akshbansal7715
      @akshbansal7715 3 роки тому

      We can add 0 at the end of the array.

  • @harshitsaini15
    @harshitsaini15 4 роки тому +1

    This is one hell of a notorious problem I have ever seen and the most fabulous explanation given for it :)

  • @sakibthetech
    @sakibthetech 4 роки тому

    best explanation..Thanks

  • @sumanreddyg8245
    @sumanreddyg8245 5 років тому

    @ 7:46 you explained [R-L-1] * hight of the minimum current bar popped from stack , again in the diagram L = 0 and R=4 then its [4-0-1]*3 = 9 right .. and why there is extra -1 in the formula [R-L-1] , also i understood L and R bars height is less than the current bar popped up from the stack please explain this point , apart from this every thing is understood

    • @anshusharma1983
      @anshusharma1983 5 років тому

      If index 4 is the right hinderance and 0 index is the left hinderance. Then the rectangle consists of indexes 1, 2 & 3. Hence (R-L) -1 i.e. (4-0)-1 as the left is not inclusive, it is the hinderance before the last bar. Hope this helps

  • @imalive404
    @imalive404 5 років тому +3

    By far the best video on this problem. Kudos _/\_

  • @agrox29
    @agrox29 4 роки тому +2

    Glad to see so many paid comments here

    • @bvsrevanth6856
      @bvsrevanth6856 4 роки тому

      I actually like their explanation ,but this is one of the worst explanation I have ever seen

  • @HumorHubIndi
    @HumorHubIndi 4 роки тому

    Thanks a lot..grt explanation..excellent work!!

  • @piyushjaiswal4866
    @piyushjaiswal4866 4 роки тому +1

    Example is same as in GFG article

  • @jeeruajay2111
    @jeeruajay2111 6 років тому

    Very good Explination

  • @ajaygaur3392
    @ajaygaur3392 5 років тому +4

    explanation starts at 11:00 and it is great

  • @VishalKumar-kr9me
    @VishalKumar-kr9me 5 років тому +1

    WHAT IF THE CURRENT NUMBER IS EQUAL TO THE TOP OF THE STACK???????

  • @sanketkale730
    @sanketkale730 4 роки тому

    Thnax man...really helpful!! keep it up!

  • @MaulikSakhida
    @MaulikSakhida 5 років тому +2

    7:45 Area = (R - L - 1) * Height of current bar, So It should be, Area = (4 - 0 - 1 ) * 3 ?

    • @VishalKumar-kr9me
      @VishalKumar-kr9me 5 років тому

      Yes you are r8...He has written that by mistake...

  • @ShubhamKumar-rt8nb
    @ShubhamKumar-rt8nb 5 років тому +1

    Finally I understood it

  • @giangkhuat9343
    @giangkhuat9343 5 років тому +2

    Thanks a lot ! I was able to follow through and wrote the code myself. Best explaining video so far on this problem

    • @CodingBlocksIndia
      @CodingBlocksIndia  5 років тому

      Thanks Glang for the compliment. please subscribe for more such videos.

  • @farhan787
    @farhan787 4 роки тому

    For O(n^2) solution, how would I be able to know that what is the minimum height bar in (i, j) range ? at 2:30

    • @MohitKumar-yr4rl
      @MohitKumar-yr4rl 4 роки тому

      My solution
      #include
      using namespace std;
      int local_min(int arr[],int i,int j)
      {
      int min=arr[i];
      for(int p=i+1;parr[p])
      min=arr[p];
      }
      return min;
      }
      int main()
      {
      int arr[]={6,2,5,4,5,1,6};
      int n=7;
      int area=0;
      for(int i=0;i

    • @rhythmbhatia8906
      @rhythmbhatia8906 4 роки тому

      // Largest area under histogram
      // O(n^2)
      #include
      #include
      using namespace std;
      int histogram(int a[],int n)
      {
      int min,area,i,j,ans=0;
      for(i=0;in;
      for(i=0;i>a[i];
      cout

  • @devcoder3107
    @devcoder3107 4 роки тому

    Please add code section in description.

  • @RAJPATEL-nm9nz
    @RAJPATEL-nm9nz 4 роки тому +1

    very good eplanation bt initial 10mins are boring.

  • @jeeruajay2111
    @jeeruajay2111 6 років тому +1

    Sir, I didn't find this type of video. Extraordinary ,,super,,

  • @ArpitDhamija
    @ArpitDhamija 5 років тому

    How to open the source code on GitHub

  • @yashbhardwaj7756
    @yashbhardwaj7756 3 роки тому

    please provide the code as well

  • @ManishKumar-zt5sk
    @ManishKumar-zt5sk 5 років тому

    great explanation once again!!!!!!!!!

  • @adityavandansharma1575
    @adityavandansharma1575 4 роки тому

    * shows gratitude *

  • @The_Helping_Hand_1997
    @The_Helping_Hand_1997 6 років тому

    Thanks Sir...

  • @riddhidutta7411
    @riddhidutta7411 6 років тому

    I coded according to your algorithm...ideone.com/LjOjXm (my solution) but in www.spoj.com/problems/HISTOGRA/ its giving tle....pls help

  • @suharajsalim4549
    @suharajsalim4549 4 роки тому

    Thanks a lot :)

  • @Great_one1
    @Great_one1 5 років тому

    can u please prove us the code?

  • @ayushaggarwal4714
    @ayushaggarwal4714 4 роки тому

    You just can't do this on your own right?

  • @piyushjaiswal4866
    @piyushjaiswal4866 4 роки тому +1

    This is not any valid reason that pop and push only once make it O(n)

  • @sambhumahato8320
    @sambhumahato8320 4 роки тому

    wow!

  • @Oswee
    @Oswee 6 років тому

    Aaaand.... what is the real-world use case of this? Why would i need this?

  • @MDMIRU152
    @MDMIRU152 2 роки тому

    Ok

  • @ManishThakur-jp7zm
    @ManishThakur-jp7zm 4 роки тому

    watch at *1.5, your welcome!

  • @yatharth_er
    @yatharth_er 6 років тому

    nice

  • @ayushthakur733
    @ayushthakur733 4 роки тому

    came here with some expectations , explanation is not good i didn't understand any part of it

  • @sommayghosh4617
    @sommayghosh4617 4 роки тому

    worth watch for 30 mins !

  • @premangraithatha8273
    @premangraithatha8273 4 роки тому

    i don't understand anything

  • @abhishekagarwal2522
    @abhishekagarwal2522 6 років тому

    What if the first bar is of 100 units alone.

  • @TheRkrrockstar
    @TheRkrrockstar 4 роки тому

    so many ads

  • @nicolasgoosen5142
    @nicolasgoosen5142 3 роки тому

    Thanks for the useful video, but, man, I never heard such a monotonous voice drawing out it's words in such a dull and languid way. Sheesh!

  • @ciphertester1147
    @ciphertester1147 6 років тому +2

    Thanks for making your video but your area calculation creates too much magic. If you code this you will find your containers are missing important data. Using a stack you need to keep the height as well as the valid starting point. Using your example the data would look like these pairs (6,0),(2,1),(5,2),(4,2),(1,0),(6,6). It would be to your benefit to code this before teaching/explaining to others. Here's a test case I used : {2,6,3,2,5,5,5,4,4,5,1,7}. Happy Hacking!

  • @shauvikpujari9522
    @shauvikpujari9522 4 роки тому

    Explanation is outstanding keep it up bro👍