Largest Area Histogram | Solution

Sdílet
Vložit
  • čas přidán 18. 04. 2020
  • Please consume this content on nados.pepcoding.com for a richer experience. It is necessary to solve the questions while watching videos, nados.pepcoding.com enables that.
    NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. In this video, we explain the solution of the problem Largest Area Histogram. To understand the question, click here: • Largest Area Histogram...
    For a better experience and more exercises, VISIT: www.pepcoding.com/resources/o...
    #pepcoding #programming #coding
    Have a look at our result: www.pepcoding.com/placements
    Follow us on our FB page: / pepcoding
    Follow us on Instagram: / pepcoding
    Follow us on LinkedIn: / pepcoding-education

Komentáře • 133

  • @its_me3716
    @its_me3716 Před 3 lety +44

    Sir, you are so calm while explaining even each smallest point..appreciation and gratitude to most heights..

    • @Pepcoding
      @Pepcoding  Před 3 lety +12

      Thank you for appreciating.
      The love and respect which I get from you people keep me highly motivated and the same I am able to forward It to you people through my videos.
      So, keep motivating, keep learning and keep loving Pepcoding😊

  • @arvindersingh9588
    @arvindersingh9588 Před 3 lety +2

    Best explanation ever. Now I realized why you recommend thinking about other variations of the next greater element on right using stack video.

  • @bobbykuhikar5527
    @bobbykuhikar5527 Před 2 lety +1

    And this is the correct way to explain
    Thankyou so much

  • @GannaJuiceEnthusiast
    @GannaJuiceEnthusiast Před 3 lety +27

    arr[i]

  • @nischayrawat6216
    @nischayrawat6216 Před 3 lety +3

    Sir it was a bit complex, but your dry run, is like ..... fab

  • @ayushmaniar9011
    @ayushmaniar9011 Před 3 lety +6

    This is perhaps the best explaination out there for this question. Thanks a ton sir, subscribing to your channel now !!!

  • @kunalkheeva
    @kunalkheeva Před rokem

    Thank you, I missed some of the stack part, but the overall video is the best.

  • @akhilagrawal4559
    @akhilagrawal4559 Před 3 lety +8

    Sir, I had read the solution to this question from many sites but never manage to learn this concept but your videos are great sir. AWESOME>>>>>>>>>>>>>>>>>>>... ThankYou Sir.

    • @Pepcoding
      @Pepcoding  Před 3 lety +3

      Keep watching. Will you also like to drop us a review?

  • @shohaghrudra4778
    @shohaghrudra4778 Před rokem

    Really good and thank you very much for your easy explanation

  • @NishantKumar-ve4oo
    @NishantKumar-ve4oo Před 3 lety +3

    For test case [2,4].The array will overflow.So push arr.length - 1 to the stack instead of arr.length initially while filling rb.

  • @donothing247
    @donothing247 Před 4 měsíci

    was struggling to form mono stack , thanks for explaining the code

  • @dsa3334
    @dsa3334 Před 2 lety

    Thank you so much sir.

  • @ranjeetgupta2442
    @ranjeetgupta2442 Před rokem

    Thank you 🙏

  • @iitdelhiassignments7271

    Sir U deserve millions of subscribers. I think you are the best on youtube.

  • @ritikdubey3338
    @ritikdubey3338 Před 3 lety

    Amazing explanation

  • @SushantKumar-ev5uh
    @SushantKumar-ev5uh Před 3 lety

    very nice question ! ekdum pyara sa sawal !

    • @Pepcoding
      @Pepcoding  Před 3 lety +1

      Thankyou beta!
      I am glad you liked it. I also hope that you are watching till the end and trying to understand the what, how, and especially why of the problem. If you like our efforts, will you like to review us here - g.page/Pepcoding/review?rc

  • @SaurabhSuman-wz1ex
    @SaurabhSuman-wz1ex Před 3 lety +1

    thanks sir, nice explanation but it is not working for long long data types. getting segmentation fault because we are using 3 arrays and 1stack. and also other variables of long long data types

  • @prachalpatel9081
    @prachalpatel9081 Před 2 lety +14

    I really enjoyed the explain from this video. However, I tried the same approach on leetcode but it failed for cases where there are multiple contagious duplicates like 11111 or 11112222. In both the while loops the conditions should be arr[i]

  • @ditu-kunalsarda6863
    @ditu-kunalsarda6863 Před 3 lety

    Sir, same code line no 46, if lb[i] = n;
    then also it gives 10/10 did it mistakenly but unsterstood

  • @anjneykumarsingh4461
    @anjneykumarsingh4461 Před 3 lety +8

    Stack queue m bdi tgde sawal h, Thanku sir

  • @devanshkumar3816
    @devanshkumar3816 Před 2 lety +4

    Sir, you just made it a piece of cake. Thank you for this crystal clear explaination!🙌🙌

    • @Pepcoding
      @Pepcoding  Před 2 lety +1

      Thank you so much, We are glad that it could be helpful. For better experience visit on nados.pepcoding.com
      Also follow our Instagram account to stay tuned.
      instagram.com/pepcoding/

    • @vinamrasangal8436
      @vinamrasangal8436 Před rokem

      @@Pepcoding meri dadi bhi aapka channel dekhti hai

    • @nivealokhande2153
      @nivealokhande2153 Před rokem +1

      @@vinamrasangal8436 ab dadi bnegi coder🤣

  • @ribhumukherjee8877
    @ribhumukherjee8877 Před 3 lety +5

    class Solution {
    public int largestRectangleArea(int[] arr) {

    int n= arr.length;

    int[] lb= new int[n]; //next smaller element on left
    int[] rb= new int[n]; //next smaller element on right

    //calculate rb using stack
    Stack s= new Stack();
    rb[n-1]=n; s.push(n-1);

    for(int i=n-2; i>=0; i--)
    {
    while(!s.isEmpty() && arr[i]

  • @chandankumar-xg4lc
    @chandankumar-xg4lc Před 3 lety

    thanks sir so much...

    • @Pepcoding
      @Pepcoding  Před 3 lety

      Thankyou beta!
      I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem.
      If you like our efforts, will you like to write a few words about us here (www.quora.com/How-do-I-start-learning-or-strengthen-my-knowledge-of-data-structures-and-algorithms )

  • @guneettalwar2630
    @guneettalwar2630 Před 3 lety

    Wonderful Explanation sir

    • @Pepcoding
      @Pepcoding  Před 3 lety

      Thankyou beta!
      Will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms)

  • @kalashnikov203
    @kalashnikov203 Před 3 lety

    By far the best explanation !!

    • @Pepcoding
      @Pepcoding  Před 3 lety

      Thankyou beta!
      I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem.
      If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )

    • @kalashnikov203
      @kalashnikov203 Před 3 lety

      @@Pepcoding Sure sir, more than happy to do it 🙂

  • @chandradevyaduraj6046
    @chandradevyaduraj6046 Před 3 lety

    Fantastic explanation sir..
    But u are not considering equal hights.

  • @atharvakalele37
    @atharvakalele37 Před 2 lety +1

    Best DSA course on CZcams

    • @Pepcoding
      @Pepcoding  Před 2 lety +1

      Please visit nados.pecoding.com for more such amazing content :)

  • @itsmechicpalak
    @itsmechicpalak Před 2 lety

    Sir my outputs are always giving an error "Exception in thread 'main'Java. Lang. Array". Can sir or anyone please me out? Plz sir!

  • @sarabpreetsingh6915
    @sarabpreetsingh6915 Před 3 lety

    ek no. explanation bhaii

  • @Web-mp7mp
    @Web-mp7mp Před 3 lety

    did this one using alternate approach for both boundaries ...... but what shoud be the default approach??

    • @Pepcoding
      @Pepcoding  Před 3 lety

      Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. Therefore, we have a premium facility available for the students in which you can get the 12 hours doubt support facility. Jisme aap agr kisi bhi question main kahin bhi faste ho to aap doubt support par reach kar skte ho aur aapko TA assign ho jayega and you can get your doubt resolved from them.

  • @swastikmishra739
    @swastikmishra739 Před 2 lety

    Sir your explanation is really good

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Keep watching and for better experience and well organised content sign up on nados.io and start learning.

  • @manglaprasadpandey8474

    Awesome explanation sir

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Glad you liked it!
      Keep learning.
      And for better experience, visit nados.io, where you will get well curated content and career opportunities.

  • @faizanhaider3953
    @faizanhaider3953 Před rokem

    11:36 bhari sambhavna hai 😂😂 sir your teaching is on Baap level 😎

  • @VaibhavBhanawatvaibhan

    How its time complexity is O(N) ?

  • @VishalKumar-ix4sr
    @VishalKumar-ix4sr Před 2 lety

    Love you sir

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Love from Pepcoding, for better experience and well organised content sign up on nados.io

  • @shubhamdeshmukh9287
    @shubhamdeshmukh9287 Před 3 lety +1

    wont work for test case int ar[] = {1,1,1,1,1}; unless you do ar[s.top()] >= ar[i], Correction is copmparision should be >= . everything else is great.

  • @roshansharma291
    @roshansharma291 Před 2 lety

    sir, we were expected to push index , you have mistakenly pushed values while dry run. (code is correct) 15:15

  • @gaurang7765
    @gaurang7765 Před 2 lety

    This is the best expalination Thank you very much sir please keep making such solution videos loved it :)

  • @pankajsinghpatwal
    @pankajsinghpatwal Před 2 lety

    Sir ❤️❤️❤️

  • @milimishra6447
    @milimishra6447 Před 3 lety

    u r awesome... the way u explain is so calm n relaxing😇😇

    • @Pepcoding
      @Pepcoding  Před 3 lety +1

      Thank you so much 😀 Keep learning, Keep growing and keep loving Pepcoding!😊

  • @harshmishra2625
    @harshmishra2625 Před 3 lety

    Sir , if given input is in the form of a String("6 , 2 , 5 , 4 , 5 , 1 , 6") , then how we can solve this problem by using the same concept.

    • @Pepcoding
      @Pepcoding  Před 3 lety +1

      string ko split karo, array milega strings ka, har element ko integer mei convert karo.

    • @harshmishra2625
      @harshmishra2625 Před 3 lety

      @@Pepcoding Thank you sir...😊

  • @rishabhgoyal2835
    @rishabhgoyal2835 Před 3 lety +1

    sir at 2:19 apne kha ki n^2 ka time hojayega lekin last me O(n) ka hogya ....approach ache se amaj aagye lekin yeah time complexity samaj nhi are isme ,,.. @pepcoding

  • @dixitasharma2912
    @dixitasharma2912 Před 3 lety

    sir,why we write arr[i]

    • @anshulgarg6146
      @anshulgarg6146 Před 3 lety

      because hum stack mein index add kr rahe hai...value nhi..

  • @xpresssoul82
    @xpresssoul82 Před 3 lety

    are we taking two stacks in this question.??..one for right boundary and one for left boundary

  • @shivam7929
    @shivam7929 Před 3 lety

    Sir your way of explaining things is next level 😍😍

    • @Pepcoding
      @Pepcoding  Před 3 lety

      I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem.
      If you like our efforts, we request a review
      g.page/Pepcoding/review?rc
      You can subscribe to our channel here
      czcams.com/users/Pepcodingabout?view_as=subscriber

    • @shivam7929
      @shivam7929 Před 3 lety

      @@Pepcoding done sir 5⭐

  • @sandeepsuthar4582
    @sandeepsuthar4582 Před rokem

    I think we should remove element that is equal and grater than current element. Test Case [6,4,5,4,7]

  • @raj-nq8ke
    @raj-nq8ke Před 3 lety

    Guys he updated the test cases and corrected the = case.

  • @MANOJKUMAR-mb2uw
    @MANOJKUMAR-mb2uw Před rokem

    for(int i = 0 ; i= 0 && a[left] >= a[i] ) {
    leftcount+=1 ;
    left-=1 ;
    }
    while(right < n && a[right] >= a[i] ) {
    rightcount += 1 ;
    right+=1 ;
    }

    max = Math.max(max , (leftcount + rightcount + 1) *a[i] ) ;
    }
    System.out.println(max) ;

  • @ujjwaljain9780
    @ujjwaljain9780 Před 3 lety

    sir wht is the time complexity of this solution??

  • @akshatgupta1092
    @akshatgupta1092 Před 3 lety

    Best teacher 🔥

    • @Pepcoding
      @Pepcoding  Před 3 lety +1

      Glad to know that you liked the content and thank you for appreciating.
      The love and respect which I get from you people keep me highly motivated and the same I am able to forward It to you people through my videos.
      So, keep motivating, keep learning and keep loving Pepcoding😊

  • @sarthaknikhal5540
    @sarthaknikhal5540 Před 2 lety

    There is an audio delay in the video

  • @aniruddhachunne3011
    @aniruddhachunne3011 Před 2 lety

    i think sir there is some problem with the voice of the video

  • @samirray7078
    @samirray7078 Před 3 lety

    Great explanation 👌👌👌

  • @dasdebajoy777
    @dasdebajoy777 Před 3 lety +27

    Instead of arr[i] < arr[st.peek()] it should be arr[i]

  • @prentitiousnoob7508
    @prentitiousnoob7508 Před 4 lety +2

    Sir please provide the code also..

    • @jonusbrothers2067
      @jonusbrothers2067 Před 4 lety +1

      code is available on pepcoding website..under solution of this question

  • @shreyankshrestha9274
    @shreyankshrestha9274 Před 2 lety

    pura lecture 40 % samj aya but dry run 100 % samj aagaya

  • @ibrahimshaikh4119
    @ibrahimshaikh4119 Před rokem

    can anyone code this in python

  • @VishalKumar-ix4sr
    @VishalKumar-ix4sr Před 2 lety

    Leetcode 84

  • @aahanaganjewar9951
    @aahanaganjewar9951 Před 2 lety +1

    Leetcode 84
    class Solution {
    public int largestRectangleArea(int[] heights) {
    int maxArea = Integer.MIN_VALUE;
    int[] lb = new int[heights.length]; // next smaller element to left
    Stack st = new Stack();
    st.push(0);
    lb[0] = -1;
    for (int i = 1; i < heights.length; i++) {
    while (st.size() > 0 && heights[st.peek()] >= heights[i]) {
    st.pop();
    }
    if (st.size() == 0) {
    lb[i] = -1;
    } else {
    lb[i] = st.peek();
    }
    st.push(i);
    }

    st = new Stack();
    int[] rb = new int[heights.length];
    rb[rb.length - 1] = heights.length;
    st.push(heights.length - 1);
    for (int i = heights.length - 2; i >= 0; i--) {
    while (st.size() > 0 && heights[st.peek()] >= heights[i]) {
    st.pop();
    }
    if (st.size() == 0) {
    rb[i] = heights.length;
    } else {
    rb[i] = st.peek();
    }
    st.push(i);
    }
    // for (int val : lb) {
    // System.out.print(val + " ");
    // }
    // System.out.println();
    // for (int val : rb) {
    // System.out.print(val + " ");
    // }
    // System.out.println();
    for (int i = 0; i < heights.length; i++) {
    int width = rb[i] - lb[i] - 1;
    int area = width * heights[i];
    if (area > maxArea) {
    maxArea = area;
    }
    }
    return maxArea;
    }
    }

  • @yuganshjain2519
    @yuganshjain2519 Před 2 lety

    class Solution {
    public int largestRectangleArea(int[] arr) {
    int[] rb = new int[arr.length];
    Stack st = new Stack();
    st.push(arr.length-1);
    rb[arr.length-1] = arr.length;
    for(int i=arr.length-2;i>=0;i--){
    while(st.size()>0 && arr[i]

  • @letsdoeverythinginoneweek9398

    i think voice and video syncing mismatch

  • @abhinavsahai3064
    @abhinavsahai3064 Před 2 lety

    Was hectic till dry run started then all sorted 👌

  • @princemakadiya9443
    @princemakadiya9443 Před 2 lety

    Taliya bajti raheni chahiye..

  • @imavij12
    @imavij12 Před 2 lety

    Watch last minute of this video if your test cases are failing

  • @pritamsaini2918
    @pritamsaini2918 Před 2 lety

    answer to 12 aana chaiye na compiler me 16 kyu dikha rha hai?

  • @harshpandey7605
    @harshpandey7605 Před 3 lety +2

    Sir sirf yhi q ne pura din mera consume kar lia
    6q per day ka target nhi ho pata aese questions ki wajah se
    Please guide

    • @saarimkhan557
      @saarimkhan557 Před 3 lety +1

      yaar yeh waali technique level up waale course Mein use karna abhi foundation mein dhang se samjho ki kaise type ke questions hote hai ek particular topic ke

    • @harshpandey7605
      @harshpandey7605 Před 3 lety

      @@saarimkhan557 sir agr kuch q smjh toh ate hai but code nhi hore so problem toh nhi hogi naa
      i will definitely try in next videis

    • @saarimkhan557
      @saarimkhan557 Před 3 lety

      @@harshpandey7605 just try and try ,waise mai sir nhi hoon 😅

    • @harshpandey7605
      @harshpandey7605 Před 3 lety

      @@saarimkhan557 Aree nevermind
      B

    • @amandixit3555
      @amandixit3555 Před 3 lety +6

      bro you can do one thing , set a target of giving at max 15 min , if you still find it difficult , see the solution vedio upto that point jahan sir logic samjha rahe hote hain and try to code it on eclipse. jab lage ki pura solution acche dekh liya aur samajh aa gaya hai move on and write a complete in your note book explaning the appproach in most detail manner. after one day or two, pick a simmilar qtn and try slving it. iss tareke se logic dimag mei baith jayega aur tabh bhi nahi hoga to matlab tum ne ratta mara tha, to dobara samjh lo, keep hustling, keep coding

  • @shivanigupta_19
    @shivanigupta_19 Před 3 lety

    For Input:
    5
    1 2 3 4 5
    This code is not giving correct output for this input , please make correction in code.

    • @Pepcoding
      @Pepcoding  Před 3 lety

      Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.

    • @shivanigupta_19
      @shivanigupta_19 Před 3 lety +1

      @@Pepcoding I know you can't solve my doubt. I am just informing you nothing else.

    • @himanshu7bansal
      @himanshu7bansal Před 3 lety

      @@shivanigupta_19 the correct output for the given input is 9, and the code gives the same output.
      Please correct me if I am wrong.

  • @YuvrajSingh-bn4zp
    @YuvrajSingh-bn4zp Před 3 lety +1

    above code and logic is failing for the test case : 11 11 10 10 10

    • @Pepcoding
      @Pepcoding  Před 3 lety

      okay, will check

    • @YuvrajSingh-bn4zp
      @YuvrajSingh-bn4zp Před 3 lety

      @@Pepcoding did it work??

    • @GannaJuiceEnthusiast
      @GannaJuiceEnthusiast Před 3 lety

      ofc it will fail, arr[i] < arr[st.peek()] , just change this part to arr[i]

    • @ravikant-hi8mz
      @ravikant-hi8mz Před 3 lety

      @@GannaJuiceEnthusiast I tried even that, but it still fails. Please check my comment above

  • @manavmalhotra8513
    @manavmalhotra8513 Před 2 lety

    Wrong Answer
    Details
    Input
    [1,1]
    Output
    1
    Expected
    2

  • @Viralmemerxyz
    @Viralmemerxyz Před rokem

    not good solution so confusing...

  • @jatinbhatoya8420
    @jatinbhatoya8420 Před 3 lety

    code for leet code 84 --
    public static class Pair {
    int index, val;
    public Pair(int index, int val) {
    this.index = index;
    this.val = val;
    }
    }
    public int largestRectangleArea(int[] heights) {
    int immediateSmaller2Right[] = new int[heights.length];
    int immediateSmaller2Left[] = new int[heights.length];
    immediateSmaller2Right[heights.length - 1] = heights.length;
    immediateSmaller2Left[0] = -1;
    Stack st = new Stack();
    st.push(new Pair(heights.length - 1, heights[heights.length - 1]));
    for (int i = heights.length - 2; i >= 0; i--) {
    while (!st.isEmpty() && st.peek().val >= heights[i]) {
    st.pop();
    }
    if (st.isEmpty()) {
    immediateSmaller2Right[i] = heights.length;
    } else {
    immediateSmaller2Right[i] = st.peek().index;
    }
    st.push(new Pair(i, heights[i]));
    }
    st = new Stack();
    st.push(new Pair(0, heights[0]));
    for (int i = 1; i < heights.length; i++) {
    while (!st.isEmpty() && st.peek().val >= heights[i]) {
    st.pop();
    }
    if (st.isEmpty()) {
    immediateSmaller2Left[i] = -1;
    } else {
    immediateSmaller2Left[i] = st.peek().index;
    }
    st.push(new Pair(i, heights[i]));
    }
    int max = Integer.MIN_VALUE;
    for (int i = 0; i < heights.length; i++) {
    max = Math.max(max, heights[i] * (immediateSmaller2Right[i] - immediateSmaller2Left[i] - 1));
    }
    return max;
    }

  • @syednamdarhussain4511
    @syednamdarhussain4511 Před 2 lety

    I think you should use
    while(!st.empty() && heights[i]