Largest Rectangular in Histogram | Maximum Rectangular Area in a Histogram | DSA-One Course #44

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 ม.ค. 2025

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

  • @benjohn9086
    @benjohn9086 2 ปีที่แล้ว +11

    At 3:43 for the solution that runs at O(n2) complexity, Should not the while loop also check if the index is going out of bounds?
    ie: while(left>=0 && a[left]>=a[I])
    left--;
    BTW love all your videos and the way you teach!!!
    I have learned a lot watching your videos. Your are amazing!!! Bhaiya Keep it up!

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

      ya you are correct but how this solution will be expected because in question constraint is 1

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

    One of the easiest explaination people literally took an hour to explain such easy concept. Great Anuj Bhaiya respect ++

  • @GhostRider....
    @GhostRider.... 2 ปีที่แล้ว +5

    class Solution {
    public int[] prevSmaller(int heights[]){
    int leftAns[] = new int[heights.length];
    Stack st = new Stack();
    for(int i = 0; i < heights.length; i++){
    while(!st.isEmpty() && heights[st.peek()] >= heights[i]){
    st.pop();
    }
    if(st.isEmpty()){
    leftAns[i] = -1;
    }else{
    leftAns[i] = st.peek();
    }
    st.push(i);
    }
    return leftAns;
    }
    public int[] nextSmaller(int heights[]){
    Stack st = new Stack();
    int rightAns[] = new int[heights.length];
    for(int i = heights.length-1; i >= 0 ; i--){
    while(!st.isEmpty() && heights[st.peek()] >= heights[i]){
    st.pop();
    }
    if(st.isEmpty()){
    rightAns[i] = heights.length;
    }else{
    rightAns[i] = st.peek();
    }
    st.push(i);

    }
    return rightAns;
    }
    public int largestRectangleArea(int[] heights) {
    int left[] = prevSmaller(heights);
    int right[] = nextSmaller(heights);
    int ans = Integer.MIN_VALUE;
    for(int i = 0; i < heights.length; i++){
    int area = (right[i] - left[i] - 1) * heights[i];
    ans = Math.max(ans, area);
    }
    return ans;
    }
    }
    Leet Code Java Solution

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

    Superb and easy to understand. Usually I have to skip first 5-10 min of the video but your video was up to the point for all 13 minutes an 48 seconds. Thank you

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

    Anuj bhiya #ये दिल मांगे मोर वीडियो on DSA

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

    One small mistake, in that prevSmaller() function, you're updating value of ns[i] instead of ps[i] for empty stack, but rest all is very well

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

    The way you are teaching great free content is similar to "Anand Kumar Ji" Super 30 .. Thanks and all the best for your future assignment ..

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

    this question is asked in my IBM exam but they want to know the largest square in a histogram. Thnaks bhaiya.
    👍🏻👍🏻👍🏻

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

    for O(n^2) approach, while condition should have = , at 8:34

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

    waah bhai choti aur clickwait video ke liye function hi gayab kardiya, kya baat hai 👏👏👏👏👏👏👏

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

    superb explanation

  • @MR-re8pq
    @MR-re8pq 2 ปีที่แล้ว +5

    Bhai well explained.. 👍
    But code in description is different one 🥲

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

    concept may be similar to container with most water . But it is little different but analogy is same

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

    very helpfull video anuj sir

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

    Crystal clear explanation bhaiya ❣️❣️❣️

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

    Amazing. Short and clear. Thanks a lot.

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

    Ek baat to sach hai bhaiya ki aap teaching me sabhse badhiiya ho

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

    I alway come here when i did not understand it even from any yt vidoe i couldn't understand that you teach..
    Lots of love to you bhaiaya
    I hope this dsa series will be atleast 100+ videos

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

    Easy solution and easy to understand
    class Solution {
    public:
    int largestRectangleArea(vector& heights) {
    int ans=0;
    stacks;
    vectorleft(heights.size());
    vectorright(heights.size());
    left[0]=1;
    s.push(0);
    for(int i=1;i=heights[i])
    {s.pop();}
    if(s.empty())
    {left[i]=i+1;}
    else
    {left[i]=i-s.top();}
    s.push(i);
    }
    while(!s.empty())
    s.pop();
    right[heights.size()-1]=1;
    s.push(heights.size()-1);
    for(int i=heights.size()-2;i>=0;i--)
    {
    while(!s.empty() && heights[s.top()]>=heights[i])
    {s.pop();}
    if(s.empty())
    {right[i]=heights.size()-i;}
    else
    {right[i]=s.top()-i;}
    s.push(i);
    }
    for(int i=0;i

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

    I just love your teaching skils

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

      and I love your writing skills

    • @Bablu-i5j
      @Bablu-i5j 2 ปีที่แล้ว

      @@alexrcrew1975 and i love your typing skills

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

    I have been looking for the brute force approach everywhere, since even after some hint, I could not understand how to code it. Thanks for the video Anuj.

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

    thank you sir for this amazing solution

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

    very nice explanation

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

    Thank sir 👍 for your superb explanation

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

    YOU ARE GREAT SIRJI

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

    Thank you 😊😊😊💚

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

    At 11:56 there is typo. In previous smaller method line no 10 it should be ps[i]=-1, not ns[i]=-1

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

    Java Solution for this explanation
    class Solution {
    public int largestRectangleArea(int[] heights) {
    int maxArea = Integer.MIN_VALUE;
    int[] prevSmaller = getPrevSmaller(heights);
    //System.out.println(Arrays.toString(prevSmaller));
    int[] nextSmaller = getNextSmaller(heights);
    //System.out.println(Arrays.toString(nextSmaller));
    /**
    * 1 6 4 4 6 6 --> (indices of next smaller element)
    * (indices of next smaller element)
    * 2, 1, 5, 6, 2, 3 (Array of heights)
    * Indices = 0 1 2 3 4 5
    */
    int len = h.length;
    for (int i = len - 1; i >= 0; i--) {
    while (!s.isEmpty() && h[i]

  • @AdityaMishra-ji8nm
    @AdityaMishra-ji8nm ปีที่แล้ว +1

    superb

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

    you teach very nicely

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

    Thank u sir🔥🔥🔥

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

    GREAT EXPLANATION

  • @AnkitMishra-cb2dt
    @AnkitMishra-cb2dt 3 ปีที่แล้ว +15

    Where we are getting good content, we are connected to it and will remain connected.
    Note : some rain frogs running in the market, but you should keep a distance from them.
    Bhaiya, keep it up👌👌👍👍❣

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

    ekdam Jhakaash Dada !!

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

    Please make a 60day roadmap for DSA beginner to crack Microsoft linkedin level companies

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

      Are you really sure bro...u can learn total dsa in 30 days

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

      Nice joke 🤣🤣

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

      In 60 days you can learn just basics of dsa..😂😂

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

      it's been 3 years, did you get into Microsoft Linkedin?

  • @Sk-lm3kv
    @Sk-lm3kv 2 ปีที่แล้ว

    Very well explained. Thanks

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

    sir please make a video on ""how to learn Javascript""

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

    at 12:31, we are taking 4 if it is not present, so if it is not present, how are you considering it for cacluation of area?

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

      we are not accessing index 4

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

    Great Video!

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

    thanku so muchh😀

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

    why will the logic of previous smaller and next smaller work? at 6:37

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

    Anuj Bhaiya,This code fails in the testcase if array is like this 1 2 3 4 5.Resulting output is 9,but this code gives output as0.

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

    great!!! really helpful

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

    Currently I'm pursuing b.tech in cse from a well known govt engg clg where our seniors are being placed in big tech companies with huge packages but trust me you will not get these kind of lectures there

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

    well explained.

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

    Very famous question thank you sir ❤

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

    Thank you Anuj Bhaiya!
    You are a Gem! 😀

  • @MohdAqib-un3rk
    @MohdAqib-un3rk 3 ปีที่แล้ว

    thankyou sirrr ,plzz more questions practice

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

    Thank You

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

    Bhaiya,I was able to find same logic like yours but it took me around 20 mins.

  • @RajeevKumar-qh6zh
    @RajeevKumar-qh6zh ปีที่แล้ว

    How would you come to know we will put -1 at 9 position and only put in next smaller element[] why we should not take in previous one .

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

    thanks bro

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

    its not working on leetcode

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

    WOW bro ty for video

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

    GFG ka link galat Question ka hai,pls correct

  • @KamleshPatel-xe9zo
    @KamleshPatel-xe9zo 3 ปีที่แล้ว +1

    Hi Anuj Bhaiya, Thanks for all your videos they've been helping a lot..

  • @NamanGoyal-t6c
    @NamanGoyal-t6c ปีที่แล้ว +1

    bhaiya ye index (right-left-1) kyu kiya hai

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

    why we subtract 1 when we calculate the width??

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

    Hi bhaiya , please start javascript video tutorials series

  • @AbhijeetKumar-zx8oz
    @AbhijeetKumar-zx8oz 3 ปีที่แล้ว +1

    Sir please help me to prepare for amazon.

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

    Where can we get this code's link?

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

    How that formula came from and why we are using next smaller and previous smaller... Can someone tell me?

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

    getting index out of bound how can we get 9th index if array size is less than that(nextsmallest method)

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

      Write arr.length if nextSmaller not present

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

    This code not give right answer with other inputs.
    Everyone try it first. Wrong code

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

      if(ns[i] == -1) ns =n; // add this condition
      int cur = (ns[i] - ps[i] - 1) * a[i];

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

    I do not understand why we have taken -1 in the brute force solution area = (right - left - 1) *a[i] please sir help.

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

      @rahulrai it should be +1 .. he made a mistake

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

    Video on AI/ML Projects bhayia

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

    Great explanation bhaiya, but this approach is still very slow on leetcode

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

    EDIT: IT BECOMES 14
    12th comment But I want heart

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

    Can someone please explain me, why we are doing ps[i] -1 while calculating the area.?

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

      we need to calculate number of bars between ns and ps and to calculate that we are writing -1, we are excluding ns and ps because it is next smallest value.

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

    This is asked in an interview 😂❤

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

    stack mai hamne values ki jagah index kyu store kare hai ?

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

    code for this?? plz share link

  • @milindpathak-here
    @milindpathak-here 2 ปีที่แล้ว

    Great Explanation as always !!
    In your code , in below snippet did you mean ps[i] = -1 ?
    "if(s.isEmpty()){
    ns[i] = -1;
    }

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

    Brute force code
    public static int largestRectangleAreaI(int[] heights) {
    int maxArea = 0;
    for (int i = 0; i < heights.length; i++) {
    int left = i;
    int right = i;
    while (left >= 0 && heights[left] >= heights[i]) {
    left--;
    }
    while (right < heights.length && heights[right] >= heights[i]) {
    right++;
    }
    maxArea = Math.max(maxArea, (right - left - 1) * heights[i]);
    }
    return maxArea;
    }

  • @Ab-dx5nm
    @Ab-dx5nm 3 ปีที่แล้ว +2

    Sir if we create a GUI application using java swing package in computer then is it possible to run this application in Android or it is limited on computer only ? Please reply please sir.

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

    please write code side by side when u explain the logic of problem..

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

    Important question 👍 thanku... Sir

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

    Ans ans is wrong for test case [9,0]

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

    Hello Bhaiya
    while making the prevSmallest function you are inserting the indexes rather than elements
    So in while loop, the stack.peek will compare the index with a[i] rather than actual element and will cause problems

    • @ssc_-kn6os
      @ssc_-kn6os 2 ปีที่แล้ว

      stack.peek is written inside a[] so it's element only
      see the code once again

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

    sir i'm diploma pass out student .how to prapare job in MNC company

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

    Why a-b-1 ????

  • @VivekSingh-wr9vb
    @VivekSingh-wr9vb ปีที่แล้ว

    Intuitive kaise hai ye

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

    Hello sir, how we define function nextSmaller(a)??

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

      This function is defined in the previous video of this tutorial series.

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

    I have a Doubt.. Is it necessary to use stack here.. I mean can't we just use normal for loop through the array to get the minimum number's index here?

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

      Yes may be bhaiya did that with using simple loop but time Complexity was O(n2) and using stack same can be done by O(n) so.

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

    ❤️❤️🙏🙏

  • @ANKITGUPTA-te4mz
    @ANKITGUPTA-te4mz 3 ปีที่แล้ว

    Hi

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

    Coding

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

    c++ code anyone pls?

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

    samjhane se zyaada show-off krne me interest hai

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

    kuch aisa bataunga jo sunke hairan ho jaoge sab log pls batao

  • @ANKITGUPTA-te4mz
    @ANKITGUPTA-te4mz 3 ปีที่แล้ว

    Bhaii

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

    are u iitian

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

    public static int findMaxArea(int[] arr) {
    Stack stack = new Stack();
    int block = 0;
    int min = Integer.MAX_VALUE;
    int area = 0, maxArea = 0;
    for (int i = 0; i < arr.length; i++) {
    stack.push(arr[i]);
    }
    for (int i = 0; i < arr.length; i++) {
    if (stack.peek() < min) {
    area = min * block;
    if (maxArea < area) {
    maxArea = area;
    }
    min = stack.pop();
    block++;
    } else {
    block++;
    stack.pop();
    }
    }
    return maxArea;
    }
    Please check this solution also
    Thanks for your videos

    • @GhostRider....
      @GhostRider.... 2 ปีที่แล้ว

      Bhai iska koi video solution bhi h kya?

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

    bhaiya app bhagwan m believe karte ho ki nahi pls batao na pls pls pls sab log batao na please

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

    great explanation