Largest Area Histogram | Solution

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ธ.ค. 2024

ความคิดเห็น • 132

  • @its_me3716
    @its_me3716 3 ปีที่แล้ว +44

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

    • @Pepcoding
      @Pepcoding  3 ปีที่แล้ว +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😊

  • @akhilagrawal4559
    @akhilagrawal4559 4 ปีที่แล้ว +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  4 ปีที่แล้ว +3

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

  • @iitdelhiassignments7271
    @iitdelhiassignments7271 ปีที่แล้ว

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

  • @nischayrawat6216
    @nischayrawat6216 3 ปีที่แล้ว +3

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

  • @ayushmaniar9011
    @ayushmaniar9011 4 ปีที่แล้ว +6

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

    • @Pepcoding
      @Pepcoding  4 ปีที่แล้ว

      Thanks for the sub!

  • @arvindersingh9588
    @arvindersingh9588 4 ปีที่แล้ว +2

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

  • @anjneykumarsingh4461
    @anjneykumarsingh4461 4 ปีที่แล้ว +8

    Stack queue m bdi tgde sawal h, Thanku sir

    • @Pepcoding
      @Pepcoding  4 ปีที่แล้ว

      All the best

  • @atharvakalele37
    @atharvakalele37 3 ปีที่แล้ว +1

    Best DSA course on TH-cam

    • @Pepcoding
      @Pepcoding  3 ปีที่แล้ว +1

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

  • @NishantKumar-ve4oo
    @NishantKumar-ve4oo 3 ปีที่แล้ว +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.

  • @devanshkumar3816
    @devanshkumar3816 3 ปีที่แล้ว +4

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

    • @Pepcoding
      @Pepcoding  3 ปีที่แล้ว +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 ปีที่แล้ว

      @@Pepcoding meri dadi bhi aapka channel dekhti hai

    • @nivealokhande2153
      @nivealokhande2153 ปีที่แล้ว +1

      @@vinamrasangal8436 ab dadi bnegi coder🤣

  • @bobbykuhikar5527
    @bobbykuhikar5527 3 ปีที่แล้ว +1

    And this is the correct way to explain
    Thankyou so much

  • @faizanhaider3953
    @faizanhaider3953 2 ปีที่แล้ว

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

  • @kunalkheeva
    @kunalkheeva 2 ปีที่แล้ว

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

  • @shohaghrudra4778
    @shohaghrudra4778 ปีที่แล้ว

    Really good and thank you very much for your easy explanation

  • @SushantKumar-ev5uh
    @SushantKumar-ev5uh 3 ปีที่แล้ว

    very nice question ! ekdum pyara sa sawal !

    • @Pepcoding
      @Pepcoding  3 ปีที่แล้ว +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

  • @nomadicmemer
    @nomadicmemer 9 หลายเดือนก่อน

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

  • @ribhumukherjee8877
    @ribhumukherjee8877 3 ปีที่แล้ว +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]

  • @kalashnikov203
    @kalashnikov203 3 ปีที่แล้ว

    By far the best explanation !!

    • @Pepcoding
      @Pepcoding  3 ปีที่แล้ว

      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 3 ปีที่แล้ว

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

  • @Malayalam_learner
    @Malayalam_learner หลายเดือนก่อน

    Thanks a lot I understood the solution I need little practice

  • @rishabhgoyal2835
    @rishabhgoyal2835 4 ปีที่แล้ว +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

    • @Pepcoding
      @Pepcoding  4 ปีที่แล้ว +1

      beta ek baar next greater element dekhie

    • @rishabhgoyal2835
      @rishabhgoyal2835 4 ปีที่แล้ว

      ok sir

  • @swastikmishra739
    @swastikmishra739 2 ปีที่แล้ว

    Sir your explanation is really good

    • @Pepcoding
      @Pepcoding  2 ปีที่แล้ว

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

  • @Malayalam_learner
    @Malayalam_learner หลายเดือนก่อน

    Why and how ar[i]

  • @dasdebajoy777
    @dasdebajoy777 4 ปีที่แล้ว +27

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

  • @ditu-kunalsarda6863
    @ditu-kunalsarda6863 3 ปีที่แล้ว

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

  • @roshansharma291
    @roshansharma291 3 ปีที่แล้ว

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

  • @sarabpreetsingh6915
    @sarabpreetsingh6915 3 ปีที่แล้ว

    ek no. explanation bhaii

    • @Pepcoding
      @Pepcoding  3 ปีที่แล้ว

      Shukriya ji🙏

  • @prachalpatel9081
    @prachalpatel9081 3 ปีที่แล้ว +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]

  • @SaurabhSuman-wz1ex
    @SaurabhSuman-wz1ex 3 ปีที่แล้ว +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

  • @ranjeetgupta2442
    @ranjeetgupta2442 ปีที่แล้ว

    Thank you 🙏

  • @manglaprasadpandey8474
    @manglaprasadpandey8474 2 ปีที่แล้ว

    Awesome explanation sir

    • @Pepcoding
      @Pepcoding  2 ปีที่แล้ว

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

  • @shivam7929
    @shivam7929 3 ปีที่แล้ว

    Sir your way of explaining things is next level 😍😍

    • @Pepcoding
      @Pepcoding  3 ปีที่แล้ว

      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
      th-cam.com/users/Pepcodingabout?view_as=subscriber

    • @shivam7929
      @shivam7929 3 ปีที่แล้ว

      @@Pepcoding done sir 5⭐

  • @ritikdubey3338
    @ritikdubey3338 3 ปีที่แล้ว

    Amazing explanation

  • @chandradevyaduraj6046
    @chandradevyaduraj6046 3 ปีที่แล้ว

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

  • @itsmechicpalak
    @itsmechicpalak 3 ปีที่แล้ว

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

  • @MANOJKUMAR-mb2uw
    @MANOJKUMAR-mb2uw ปีที่แล้ว

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

  • @Web-mp7mp
    @Web-mp7mp 4 ปีที่แล้ว

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

    • @Pepcoding
      @Pepcoding  4 ปีที่แล้ว

      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.

  • @VaibhavBhanawatvaibhan
    @VaibhavBhanawatvaibhan 2 ปีที่แล้ว

    How its time complexity is O(N) ?

  • @dsa3334
    @dsa3334 3 ปีที่แล้ว

    Thank you so much sir.

  • @guneettalwar2630
    @guneettalwar2630 4 ปีที่แล้ว

    Wonderful Explanation sir

    • @Pepcoding
      @Pepcoding  4 ปีที่แล้ว

      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)

  • @pankajsinghpatwal
    @pankajsinghpatwal 2 ปีที่แล้ว

    Sir ❤️❤️❤️

  • @milimishra6447
    @milimishra6447 3 ปีที่แล้ว

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

    • @Pepcoding
      @Pepcoding  3 ปีที่แล้ว +1

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

  • @gaurang7765
    @gaurang7765 3 ปีที่แล้ว

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

  • @akshatgupta1092
    @akshatgupta1092 3 ปีที่แล้ว

    Best teacher 🔥

    • @Pepcoding
      @Pepcoding  3 ปีที่แล้ว +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😊

  • @VishalKumar-ix4sr
    @VishalKumar-ix4sr 2 ปีที่แล้ว

    Love you sir

    • @Pepcoding
      @Pepcoding  2 ปีที่แล้ว

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

  • @shubhamdeshmukh9287
    @shubhamdeshmukh9287 3 ปีที่แล้ว +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.

  • @dixitasharma2912
    @dixitasharma2912 4 ปีที่แล้ว

    sir,why we write arr[i]

    • @anshulgarg6146
      @anshulgarg6146 3 ปีที่แล้ว

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

  • @xpresssoul82
    @xpresssoul82 3 ปีที่แล้ว

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

    • @SaurabhSuman-wz1ex
      @SaurabhSuman-wz1ex 3 ปีที่แล้ว

      no

    • @ravikant-hi8mz
      @ravikant-hi8mz 3 ปีที่แล้ว

      yes. There are 2 stacks being created

    • @dikshajain9845
      @dikshajain9845 3 ปีที่แล้ว

      No we are using 2 arrays which sir has declared on the line 17 and 28 rb and lb .

  • @samirray7078
    @samirray7078 4 ปีที่แล้ว

    Great explanation 👌👌👌

    • @Pepcoding
      @Pepcoding  4 ปีที่แล้ว +1

      Thank you ji

  • @harshmishra2625
    @harshmishra2625 4 ปีที่แล้ว

    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  4 ปีที่แล้ว +1

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

    • @harshmishra2625
      @harshmishra2625 4 ปีที่แล้ว

      @@Pepcoding Thank you sir...😊

  • @sandeepsuthar4582
    @sandeepsuthar4582 2 ปีที่แล้ว

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

  • @chandankumar-xg4lc
    @chandankumar-xg4lc 4 ปีที่แล้ว

    thanks sir so much...

    • @Pepcoding
      @Pepcoding  4 ปีที่แล้ว

      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 )

  • @ujjwaljain9780
    @ujjwaljain9780 4 ปีที่แล้ว

    sir wht is the time complexity of this solution??

  • @raj-nq8ke
    @raj-nq8ke 3 ปีที่แล้ว

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

  • @xtylishsumitchauhan5561
    @xtylishsumitchauhan5561 3 หลายเดือนก่อน

  • @aahanaganjewar9951
    @aahanaganjewar9951 2 ปีที่แล้ว +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 2 ปีที่แล้ว

    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]

  • @prentitiousnoob7508
    @prentitiousnoob7508 4 ปีที่แล้ว +2

    Sir please provide the code also..

    • @jonusbrothers2067
      @jonusbrothers2067 4 ปีที่แล้ว +1

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

  • @shreyankshrestha9274
    @shreyankshrestha9274 3 ปีที่แล้ว

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

  • @sarthaknikhal5540
    @sarthaknikhal5540 3 ปีที่แล้ว

    There is an audio delay in the video

  • @abhinavsahai3064
    @abhinavsahai3064 3 ปีที่แล้ว

    Was hectic till dry run started then all sorted 👌

  • @aniruddhachunne3011
    @aniruddhachunne3011 2 ปีที่แล้ว

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

  • @VishalKumar-ix4sr
    @VishalKumar-ix4sr 2 ปีที่แล้ว

    Leetcode 84

  • @harshpandey7605
    @harshpandey7605 4 ปีที่แล้ว +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 4 ปีที่แล้ว +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 4 ปีที่แล้ว

      @@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 4 ปีที่แล้ว

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

    • @harshpandey7605
      @harshpandey7605 4 ปีที่แล้ว

      @@saarimkhan557 Aree nevermind
      B

    • @amandixit3555
      @amandixit3555 4 ปีที่แล้ว +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

  • @pritamsaini2918
    @pritamsaini2918 2 ปีที่แล้ว

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

  • @ibrahimshaikh4119
    @ibrahimshaikh4119 2 ปีที่แล้ว

    can anyone code this in python

  • @jatinbhatoya8420
    @jatinbhatoya8420 3 ปีที่แล้ว

    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;
    }

  • @imavij12
    @imavij12 3 ปีที่แล้ว

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

  • @shivanigupta_19
    @shivanigupta_19 4 ปีที่แล้ว

    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  4 ปีที่แล้ว

      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 4 ปีที่แล้ว +1

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

    • @himanshu7bansal
      @himanshu7bansal 3 ปีที่แล้ว

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

  • @letsdoeverythinginoneweek9398
    @letsdoeverythinginoneweek9398 3 ปีที่แล้ว

    i think voice and video syncing mismatch

  • @manavmalhotra8513
    @manavmalhotra8513 2 ปีที่แล้ว

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

  • @princemakadiya9443
    @princemakadiya9443 3 ปีที่แล้ว

    Taliya bajti raheni chahiye..

  • @YuvrajSingh-bn4zp
    @YuvrajSingh-bn4zp 4 ปีที่แล้ว +1

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

    • @Pepcoding
      @Pepcoding  4 ปีที่แล้ว

      okay, will check

    • @YuvrajSingh-bn4zp
      @YuvrajSingh-bn4zp 4 ปีที่แล้ว

      @@Pepcoding did it work??

    • @ravikant-hi8mz
      @ravikant-hi8mz 3 ปีที่แล้ว

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

  • @Viralmemerxyz
    @Viralmemerxyz 2 ปีที่แล้ว

    not good solution so confusing...

  • @syednamdarhussain4511
    @syednamdarhussain4511 2 ปีที่แล้ว

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

    • @AKASHKUMAR-we5hg
      @AKASHKUMAR-we5hg 2 ปีที่แล้ว

      correct then it passes all the test cases