Largest Rectangle in Histogram 🔥 | Stack Data structure & Algorithms Hello World Leetcode #84

Sdílet
Vložit
  • čas přidán 10. 03. 2022
  • This is the video under the series of DATA STRUCTURE & ALGORITHM in a STACK Playlist. Now we are going to solve a stack problem Largest Rectangle in Histogram Leetcode or Largest Rectangular Area in a Histogram
    Join My Telegram channel for more Updates: telegram.me/helloworldbyprince
    complete DSA preparation: github.com/Prince-1501/Comple...
    ----------------------------------------------------------------------------------------
    ► 84. Largest Rectangle in Histogram
    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.
    Input: heights = [2,1,5,6,2,3]
    Output: 10
    Explanation: The above is a histogram where the width of each bar is 1.
    The largest rectangle is shown in the red area, which has an area = of 10 units.
    ►Largest Rectangle in Histogram: leetcode.com/problems/largest...
    ► Code In this video: github.com/Prince-1501/Hello_...
    ► Pdf In this video: github.com/Prince-1501/Hello_...
    ----------------------------------------------------------------------------------------
    *Follow me *
    LinkedIn► / iamprince
    Facebook► / helloworldofficials
    Instagram► / helloworldbyprince
    Twitter► / prince_king_
    Telegram► telegram.me/helloworldbyprince
    ----------------------------------------------------------------------------------------
    ►Our Playlists on:-
    ► Tree: • Tree Data Structure & ...
    ► Stack: • Stack & Queue Data Str...
    ► Hashing: • Hashing Data Structure...
    ► Graph: • Graph Data Structure &...
    ► Matrix: • Matrix (Multidimension...
    ► STL: • Standard Template Libr...
    ► Leetcode: • LeetCode Solutions And...
    ►Competitive Programming: • Full course in Competi...
    ►C++ Full Course : • C++ full Course in HINDI
    ►Algorithms: • L-01 || Prefix Sum Arr...
    ►Data Structure: • Data Structures with C...
    ------------------------------------------------------------------------
    🌟 Please leave a LIKE ❤️ and SUBSCRIBE for more AMAZING content! 🌟
    ✨ Tags ✨
    leetcode problems
    leetcode problems java
    leetcode problems python
    leetcode problems that got me tired
    leetcode problems c++
    leetcode problems and solutions python
    leetcode problems playlist
    leetcode problems and solutions java
    leetcode problems in Hindi
    leetcode problems javascript
    leetcode problems and solutions
    leetcode problems of the day
    leetcode problems for beginners
    leetcode problems easy
    leetcode problems js
    stack practice problems
    stack practice problems gfg
    leetcode stack questions
    leetcode stack queue
    stack hello world
    Largest Rectangle in Histogram leetcode solution
    Largest Rectangle in Histogram gfg
    Largest Rectangle in Histogram
    question asked in Google
    off-campus placement
    Online Stock Span
    Practice stack data structure
    Stack in a data structure in Hindi
    Stack Full playlist for Beginners
    algorithms
    graph DSA
    data structure
    sorting algorithms
    time-complexity analysis
    gate computer science preparation
    programming languages
    #stack #Leetcode #programming

Komentáře • 54

  • @Shadow-lx9rh
    @Shadow-lx9rh Před 2 lety +8

    Sir just now fnshd viewing all ur stack videos thank you soo much
    1. For providing que in description and code link in leetcode and gfg(helps to practice ques multiple times and understand the twist)
    2. For pdf as well as code(it is very helpful when i watch in 240p coz my internet amount is less)
    3. More than anything thank you from the core of my heart really really really
    for starting the video by saying that ques is very easy and
    insisting that we first try to solve it and stopping in middle of vdeo and telling us to try the further part
    This implies for ur tree and grap playlist also as a newbie to CP i was intimidated on seeing all the complex content in yt and problems in website.
    But after knowing abt this channel in yt recommendation i feel that this was really a boon to me.
    All ur playlist Start frm scratch utterly which is very helpful
    My only request to u is that pls if possible try to make some different playlists too like dynamic prgming and other topics which u feel impo to be Known.
    When u teach i feel as if u teach straight frm ur heart genuinely pls continue this good work means a lot 🙏

  • @montuedge
    @montuedge Před 5 měsíci +1

    3:00 ❤ key point

  • @viveksoni1923
    @viveksoni1923 Před rokem

    Apke video dekhne ke baad doubt ho aisa nhi hua abbhi tak.
    Thank you for those valuable videos and do cover more leetcode solutions which are importan. Thank you

  • @harshitasingh4690
    @harshitasingh4690 Před 12 dny

    Greatt workk..Your explanation is soo good

  • @GudduKumar-fn8tb
    @GudduKumar-fn8tb Před rokem

    amazing explanation .

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

    Nice !!

  • @AkashSharma-fw1lz
    @AkashSharma-fw1lz Před 3 měsíci

    You are the best

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

    Amazing explanation,loved it!!

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

    Nice aise pdhaya karo har question sir naive to optimise. Best 👍😂👍

  • @LionHeart1104
    @LionHeart1104 Před 10 měsíci

    GREAT WORK, THANKS SO MUCH...

  • @SomneelMaity
    @SomneelMaity Před 2 lety

    Thanks Bhaiya, this video was really helpful, I solved the question in O(N) when you said "Do It Yourself".

  • @rajankhunt7002
    @rajankhunt7002 Před rokem

    This video quality is soo good. please consistency maintain this.

  • @dipanshuraj7868
    @dipanshuraj7868 Před 5 měsíci

    bhaiya I have a doubt about how to find the time complexity of this code could you explain it?

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

    Gotcha greate Explaintion.
    Try to upload daily atleast 1 video if possible

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

    Nice explanation sir

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

    Amazing explanation 🥺

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 2 lety

      bass aise hi padte raho
      Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀

  • @vaibhavgupta973
    @vaibhavgupta973 Před rokem

    thx

  • @user-ok8lm5wd7i
    @user-ok8lm5wd7i Před rokem

    maja hi aagaya es question par , time laga but aacha question tha

  • @shaurya2608
    @shaurya2608 Před rokem

    🔥🔥

  • @narendra7556
    @narendra7556 Před rokem +1

    amazing one

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před rokem +1

      Thank you! Cheers!
      Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀

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

    How can we find for a largest square in a histogram? What changes can we make?

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

      Nice question ✅
      Consider every bar as small
      Now, count the number of bars ...which can make form rectangle
      Now, check if
      count of bar >= height of bar
      Then a square can be formed ...
      Then area = height * height

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

    Nice sir

  • @aniketkumar2502
    @aniketkumar2502 Před rokem +1

    jabardast explanation! mza aa gya🤩🤩🤩🤩

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

    why this problem feels like container with most water on gfg .

  • @1anuragmishra01
    @1anuragmishra01 Před rokem

    Osm BHAIYA 🫂🙏

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

    Hi, Subhankar. I have 6 years of experience in TCS. Is it possible to move to product based company. Will they consider my profile

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

      Offcourse it is possible and they will definitely consider your profile

    • @bholadas31
      @bholadas31 Před 10 měsíci

      ​@@HelloWorldbyprince❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤q❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤

  • @only_for_fun1234r
    @only_for_fun1234r Před rokem

    i have written the same code and its working also but after submitting its showing TLE when all values are same please help
    my code
    class Solution {
    public:
    vector right_min(vector& h)
    {
    vector ans2;
    stack st;
    int n=h.size();
    for(int i=n-1;i>=0;i--)
    {
    while(!st.empty() && h[st.top()]>=h[i])
    st.pop();
    int res=(st.empty()) ? n:st.top();
    ans2.push_back(res);
    st.push(i);
    }
    reverse(ans2.begin(),ans2.end());
    return ans2;
    }
    vector left_min(vector& h)
    {
    vector ans1;
    stack st;
    int n=h.size();
    for(int i=0;i=h[i])
    st.pop();
    int res=st.empty() ? -1:st.top();
    ans1.push_back(res);
    st.push(i);
    }
    return ans1;
    }
    int largestRectangleArea(vector& h) {
    int maxi=-1;
    vector ans1=left_min(h);
    vector ans2=right_min(h);
    for(int i=0;i

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

    Bhaiya please make a video on your resume please 🙏

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

      Exactly kya cahhaiye resume wali video me

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

      @@HelloWorldbyprince apne resume me kya achivement add ki thi project kese explain Kiya tha format konsa tha , agar sirf Leetcode Kiya hai to achivement kya likhe iss se releted

  • @priyanshuthakur8947
    @priyanshuthakur8947 Před rokem

    class Solution {
    public int largestRectangleArea(int[] heights) {
    int n=heights.length;
    Stackst=new Stack();
    int[] ps=new int[n];
    int[] ns=new int[n];
    for(int i=0;i=heights[i]){
    st.pop();
    }
    if(st.isEmpty()){
    ps[i]=-1;
    }else{
    ps[i]=st.peek();
    }
    st.push(i);
    }
    while(!st.isEmpty()){
    st.pop();
    }
    for(int i=n-1;i>=0;i--){
    while(!st.isEmpty()&&heights[st.peek()]>=heights[i]){
    st.pop();
    }
    if(st.isEmpty()){
    ns[i]=n;
    }else{
    ns[i]=st.peek();
    }
    st.push(i);
    }
    int max=0;
    for(int i=0;i

  • @showsshorts4696
    @showsshorts4696 Před rokem +1

    sir i think in naive approach curr+=arr[i] ki jagah cur+=cur; ayeegaa.....

  • @Maheshwaripremierleague

    int get_max_area(vector a)
    {
    stack st;
    int n = a.size(),ans=0,i=0;
    a.push_back(0);
    while(ia[i])
    {
    int h = a[st.top()];
    st.pop();
    if(st.empty())
    {
    ans = max(ans,h*i);
    }
    else
    {
    int len = i-st.top()-1;
    ans = max(ans,h*len);
    }
    }
    st.push(i);
    i++;
    }
    return ans;
    }

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

    My Approach :
    class Solution {
    public:
    int largestRectangleArea(vector& heights) {
    /* BRUTE FORCE : O(N*N) using array data structure only and for loop
    int n=heights.size();
    vector pre(n,-1),post(n,-1);
    //finding pre
    for(int i=n-1;i>=0;i--)
    {
    bool check=false;
    for(int j=i-1;j>=0;j--)
    {
    if(heights[i]>heights[j])
    {
    pre[i]=j;
    check=true;
    break;
    }
    }
    if(check==true)
    check=false;
    }
    //finding post
    for(int i=0;i