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

Sdílet
Vložit
  • čas přidán 7. 09. 2024
  • Hey guys, In this video, We're going to solve another very famous Interview Problem called - The largest rectangular area in a Histogram
    code: www.geeksforge...
    🥳 Join our Telegram Community:
    Telegram channel: telegram.me/re...
    Telegram group: telegram.me/ds...
    🚀 Follow me on:
    Instagram: / anuj.kumar.sharma
    Linkedin: / sharma-kumar-anuj
    Twitter: / realanujbhaiya
    💸 Use coupon code ANUJBHAIYA on GeeksforGeeks to avail discounts on courses!
    📚 Complete DSA Playlist: • DSA-One Course - The C...
    Complete Android Development Playlist: • Android Development Tu...
    Hashtags:
    #anujbhaiya #dsaone
    Ignore these tags:
    largest rectangle in histogram
    maximum rectangular area in a histogram
    largest area of the histogram
    anuj bhaiya
    84. largest rectangle in histogram
    largest area in histogram
    largest rectangular area in a histogram
    histogram
    max rectangle
    largest area histogram
    maximum area histogram
    maximum area of histogram
    largest histogram
    largest rectangular area in histogram
    maximal rectangle
    leetcode 84
    area of histogram
    maximum rectangular area in histogram
    max area histogram
    largest rectangle
    dsa
    largest rectangle in histogram leetcode
    maximum size rectangle
    anuj bhaiya dsa
    max area of histogram
    largest rectangle hackerrank
    monotonic stack
    largest area of histogram
    max area in histogram
    stack in java
    anuj bhaiya java
    largest rectangle in histogram leetcode #84
    anuj kumar sharma
    java
    largest rectangle area in histogram
    largest square in the garden codechef solution
    largest square possible hackerrank
    max rectangle gfg
    maximum rectangle
    merge sort
    anuj
    anuj bhai
    dsa anuj bhaiya
    dsa in java
    dsa in python
    given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram.
    histogram area
    la vie en rose
    largest area of rectangle with permutations
    largest area rectangular sub-matrix with equal number of 1’s and 0’s
    largest rectangle histogram
    largest rectangle in a histogram

Komentáře • 121

  • @benjohn9086
    @benjohn9086 Před rokem +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 Před měsícem

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

  • @parthchittora1128
    @parthchittora1128 Před 10 měsíci +1

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

  • @priyanshumohanty5261
    @priyanshumohanty5261 Před 2 lety +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

  • @KushalBhatia
    @KushalBhatia Před 2 lety +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

  • @GhostRider....
    @GhostRider.... Před rokem +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

  • @shyamagrawal3047
    @shyamagrawal3047 Před 2 lety +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 ..

  • @MR-re8pq
    @MR-re8pq Před 2 lety +5

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

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

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

  • @ameygadhe6437
    @ameygadhe6437 Před 11 měsíci +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

  • @tech_wizard9315
    @tech_wizard9315 Před 2 lety +33

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

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

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

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

      Nice joke 🤣🤣

    • @069yaswanthreddy2
      @069yaswanthreddy2 Před rokem +1

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

  • @parthanuj8611
    @parthanuj8611 Před rokem +1

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

  • @vishalpatil4983
    @vishalpatil4983 Před 2 lety +7

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

    • @Rohit35190
      @Rohit35190 Před 2 lety

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

  • @DebojyotiMandal
    @DebojyotiMandal Před 2 lety +2

    Crystal clear explanation bhaiya ❣️❣️❣️

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

    Amazing. Short and clear. Thanks a lot.

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

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

  • @akankshayadav1041
    @akankshayadav1041 Před 2 lety +2

    very helpfull video anuj sir

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

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

  • @algosandds8215
    @algosandds8215 Před 2 lety +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]

  • @AnkitMishra-cb2dt
    @AnkitMishra-cb2dt Před 2 lety +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👌👌👍👍❣

  • @divyagupta6854
    @divyagupta6854 Před 2 lety +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.

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

    superb explanation

  • @nandanex4916
    @nandanex4916 Před 2 lety +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.

  • @vivekgupta707
    @vivekgupta707 Před 2 lety +2

    Thank sir 👍 for your superb explanation

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

    its not working on leetcode

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

    very nice explanation

  • @prashantshukla6870
    @prashantshukla6870 Před 2 lety

    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

  • @myyoutubeisthis
    @myyoutubeisthis Před 3 měsíci

    thank you sir for this amazing solution

  • @himanshuladva8127
    @himanshuladva8127 Před 2 lety +2

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

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

    I just love your teaching skils

  • @RajeevKumar-qh6zh
    @RajeevKumar-qh6zh Před rokem

    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 .

  • @shamanthgm286
    @shamanthgm286 Před 2 lety +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

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

    YOU ARE GREAT SIRJI

  • @abhishekprasad1814
    @abhishekprasad1814 Před měsícem

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

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

    Thank you 😊😊😊💚

  • @sahilgupta7170
    @sahilgupta7170 Před 2 lety

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

  • @AdityaMishra-ji8nm
    @AdityaMishra-ji8nm Před rokem +1

    superb

  • @Sk-lm3kv
    @Sk-lm3kv Před 2 lety

    Very well explained. Thanks

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

    Thank u sir🔥🔥🔥

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

    Very famous question thank you sir ❤

  • @MangoLassiYT
    @MangoLassiYT Před 2 lety

    you teach very nicely

  • @MohdAqib-un3rk
    @MohdAqib-un3rk Před 2 lety

    thankyou sirrr ,plzz more questions practice

  • @digantachaudhuri
    @digantachaudhuri Před 2 lety

    GREAT EXPLANATION

  • @rahulrai1386
    @rahulrai1386 Před 11 měsíci

    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 Před 7 měsíci

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

  • @Strange2309
    @Strange2309 Před rokem

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

  • @kumarrohit8311
    @kumarrohit8311 Před 2 lety

    Thank you Anuj Bhaiya!
    You are a Gem! 😀

  • @ankitjoshi2302
    @ankitjoshi2302 Před 2 lety

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

  • @madanmohanpachouly6135

    well explained.

  • @ranjitkoragoankar
    @ranjitkoragoankar Před 2 lety

    Thank You

  • @rohitsengar100
    @rohitsengar100 Před 2 lety

    great!!! really helpful

  • @KamleshPatel-xe9zo
    @KamleshPatel-xe9zo Před 2 lety +1

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

  • @nafeesansari4533
    @nafeesansari4533 Před 9 měsíci

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

    • @dhruvumraliya2645
      @dhruvumraliya2645 Před měsícem

      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.

  • @gauravbanerjee2898
    @gauravbanerjee2898 Před 11 měsíci

    why we subtract 1 when we calculate the width??

  • @ankitbhatt5630
    @ankitbhatt5630 Před 2 lety

    Great Video!

  • @rohandevaki4349
    @rohandevaki4349 Před 2 lety

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

  • @user-dp7ux4sk3c
    @user-dp7ux4sk3c Před 11 měsíci +1

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

  • @baniyaindia
    @baniyaindia Před 2 lety +2

    GFG ka link galat Question ka hai,pls correct

  • @shibinez2779
    @shibinez2779 Před rokem

    thanks bro

  • @arindamdutta7369
    @arindamdutta7369 Před 2 lety

    ekdam Jhakaash Dada !!

  • @rohandevaki4349
    @rohandevaki4349 Před 2 lety

    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 Před 2 lety

      we are not accessing index 4

  • @muskanchaurasia1532
    @muskanchaurasia1532 Před rokem

    thanku so muchh😀

  • @AbhijeetKumar-zx8oz
    @AbhijeetKumar-zx8oz Před 2 lety +1

    Sir please help me to prepare for amazon.

  • @pankajmaury_
    @pankajmaury_ Před 2 lety

    Hi bhaiya , please start javascript video tutorials series

  • @pankajas6448
    @pankajas6448 Před 2 lety

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

  • @khudkikhoz
    @khudkikhoz Před 2 lety +6

    This is asked in an interview 😂❤

  • @shivammudgil3477
    @shivammudgil3477 Před 6 měsíci

    code for this?? plz share link

  • @gtbaba123
    @gtbaba123 Před rokem

    WOW bro ty for video

  • @InfinteMotivation
    @InfinteMotivation Před rokem

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

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

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

  • @afzalmohd8442
    @afzalmohd8442 Před 2 lety

    Where can we get this code's link?

  • @Ab-dx5nm
    @Ab-dx5nm Před 2 lety +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 Před rokem

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

  • @pinkkitty6553
    @pinkkitty6553 Před rokem

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

  • @milindpathak-here
    @milindpathak-here Před rokem

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

  • @TalentedSports
    @TalentedSports Před 2 lety

    Important question 👍 thanku... Sir

  • @tywinlannister2738
    @tywinlannister2738 Před 2 lety

    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 Před 2 lety

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

  • @saisravani2625
    @saisravani2625 Před 2 lety

    Video on AI/ML Projects bhayia

  • @mohammad_saud_humayun

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

  • @akankshayadav1041
    @akankshayadav1041 Před 2 lety

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

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

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

    • @AnujBhaiya
      @AnujBhaiya  Před 2 lety +2

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

  • @VivekSingh-wr9vb
    @VivekSingh-wr9vb Před 10 měsíci

    Intuitive kaise hai ye

  • @bmodi16
    @bmodi16 Před 2 lety

    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 Před 2 lety

      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.

  • @raghavsharma4195
    @raghavsharma4195 Před 2 lety

    Why a-b-1 ????

  • @ANKITGUPTA-te4mz
    @ANKITGUPTA-te4mz Před 2 lety

    Hi

  • @lokendrasingh9780
    @lokendrasingh9780 Před 2 lety

    ❤️❤️🙏🙏

  • @MrHip4hopper
    @MrHip4hopper Před 2 lety

    c++ code anyone pls?

  • @igaming_
    @igaming_ Před 2 lety

    Coding

  • @ANKITGUPTA-te4mz
    @ANKITGUPTA-te4mz Před 2 lety

    Bhaii

  • @richknowledge9235
    @richknowledge9235 Před 2 lety

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

  • @mehoneybadger999
    @mehoneybadger999 Před rokem

    samjhane se zyaada show-off krne me interest hai

  • @Yuvrjput0
    @Yuvrjput0 Před 2 lety

    are u iitian

  • @alokbhowmik3547
    @alokbhowmik3547 Před 2 lety

    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

  • @richknowledge9235
    @richknowledge9235 Před 2 lety

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