Largest Rectangle in Histogram | Part - 1 | Leetcode 84

Sdílet
Vložit
  • čas přidán 7. 08. 2024
  • Check out website: SDE sheet: bit.ly/takeUforward_SDE
    Pre-req: • Next Greater Element |...
    Part-2: • Largest Rectangle in H...
    Watch at 1.25x for better experience ..
    Visit Relevel: relvl.co/vcd
    upGrad(BD)- relvl.co/eh5
    Urban Company(BD)- relvl.co/5w0
    Vedantu(BD)- relvl.co/h3t
    Curefit(BD- relvl.co/d08
    Cred(FD)- relvl.co/fqa
    Digit(FD)- relvl.co/0vy
    Razorpay(BE)- relvl.co/fly
    Yellow Messenger(BE)- relvl.co/wu9
    Cred(BE)- relvl.co/y0h
    1mg(BE)- relvl.co/3x7
    Digit(BE)- relvl.co/wkz
    ---------------------------------------------------------------------------------------------------------------------------
    Problem Link: leetcode.com/problems/largest...
    C++/Java Code: takeuforward.org/data-structu...
    ---------------------------------------------------------------------------------------------------------------------------
    ✅Use coupon-code "STRIVER" for getting 10% on all subscriptions of Unacademy
    ✅Use coupon-code "TAKEUFORWARD" for getting 15% for all CN courses: aff.codingninjas.com/click?o=...
    ✅Use coupon-code "TAKEUFORWARD" for getting 10% for all GFG courses: bit.ly/tuf_gfgCourse​
    ✅Please Please SUBSKRIIIBEEEEE the new channel: / @striver_79
    ---------------------------------------------------------------------------------------------------------------------------
    Products that I use:
    Best Mic in Affordable Range: amzn.to/3hiTr9p
    Yeti mic: amzn.to/3dL4tDF
    Boya mic: amzn.to/3h4ROgy
    Logitech webcam: amzn.to/3h7d2KI
    Cooling pad: amzn.to/3y46pi4
    Wacom pad: amzn.to/3xacSIq
    Ring light: amzn.to/3y7aaU1
    Mic Arm Stand: amzn.to/3qzMuFa
    Digitek Green light stand: amzn.to/2U7B3bI
    Digitek Stand: amzn.to/363m7Oo
    Tripod: amzn.to/35ZmbyT
    Office chair: amzn.to/3qysu5Z
    Ipad Air: amzn.to/3hjpiXx
    Iphone 12: amzn.to/3AavyJV
    Watch: amzn.to/3hn7w5A
    Macbook Air: amzn.to/363lSTu
    Macbook Pro: amzn.to/3qCczDr
    Board: amzn.to/3x8GLIO
    Mouse: amzn.to/360Asv1
    ---------------------------------------------------------------------------------------------------------------------------
    ✅Placement Series: • Let's give back to the...
    ✅Placement Series (Arrays, Sorting..): • C++ and Java |Brute-Be...
    ✅Hashing Playlist: • Two Sum Problem | Leet...
    ✅Greedy Playlist: • N meetings In One Room...
    ✅Recursion Playlist: • L8. Combination Sum | ...
    ✅Graph Playlist: • 3 MAJOR ANNOUNCEMENTS ...
    ✅Two Pointer Playlist: • 3 SUM | Brute | Better...
    ✅Binary Search Playlist: • Nth Root of a Number U...
    ✅LinkedList Playlist: • Reverse a Linked List ...
    ✅Advanced DS playlist: • Fenwick Tree Tutorial ...
    ✅Stack&Queue Playlist: • Implementation of Stac...
    ✅Greedy Algorithms: • N meetings In One Room...
    ---------------------------------------------------------------------------------------------------------------------------
    If you appreciate the channel's work, you can join the family: bit.ly/joinFamily​
    ✅Thumbnail Creator: / rikonakhuli
    ✅ Striver's Linkedin Profile: / ​
    ✅ Instagram: / ​
    ✅Connect with us: www.google.com/search?client=... (Use Link in Mobile only, if not works search "takeUforward" in telegram)..
    ##dsa​ #striver #leetcode

Komentáře • 154

  • @manojnavinjamuri5867
    @manojnavinjamuri5867 Před rokem +9

    Thank you for such amazing explanation. I cant find a single video of others who explain with such clarity. No matter who criticize you continue the work u r doing.

  • @divyagupta6854
    @divyagupta6854 Před 2 lety +27

    One thing I want to point out is that, in interview, there are some scenarios when only brute force solution exists to the given problem. And its important to understand that even though solution is not optimal, it has given the solution indeed. So, optimal solution is not always guaranteed, even though it exists in this problem.
    Nevertheless, great video @takeUforward, the best explanation for this problem I found so far. 🤟

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

      yes, I am just bothered with too much to remember - formula and technique :(

  • @stutisuchak4525
    @stutisuchak4525 Před 2 lety +9

    I saw multiple videos on this question, but this one made it clear. Thank you so much.

  • @anshuljain1700
    @anshuljain1700 Před 3 lety +47

    Wish i could like it millions of time❤️
    Never have I ever seen such a great video❤️
    Superb explanation 👍
    Tried your best to embibe intuition👍❤️

  • @thisisankitgaur
    @thisisankitgaur Před 2 lety +37

    ❤️ from IIT Bombay. Nice Work!!

    • @trojanhorse8278
      @trojanhorse8278 Před rokem +4

      Hum log Mtech wale hai , hame dekhna padta hai :}

    • @manasgotefode
      @manasgotefode Před 9 měsíci +1

      Or bhai kaha lgi placement

    • @phantomhawk489
      @phantomhawk489 Před 7 měsíci +5

      ​@@manasgotefode bhai usse shadi karoge kya?

    • @MPrachiShinde
      @MPrachiShinde Před 3 měsíci +9

      Flex karne ka tareeka thoda kezual hai

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

    bhai the energy with which u explain incredibly amazing!

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

    Thanks Striver for such a neat explanation. Intuition is to store next smaller and previous smaller value(here index) for current value in two separate arrays and then use this approx formula currentHeight * (currentNextSmallerValue - currentPreviousSmallerValue) for current index. Take the max out of this to get desired output

  • @ankitsablok952
    @ankitsablok952 Před 2 lety +25

    Really liked the intuition you developed with boundaries. Thoroughly impressed by the way you have explained this problem. I solved this problem using the boundary approach you mentioned :). Pretty nice explanation as I can imagine its hard to put this complex a thought into appropriate words.

  • @nihalsingh6233
    @nihalsingh6233 Před 6 měsíci +3

    00:01 Understanding how to find the largest rectangle area in a histogram.
    02:09 Selecting the maximum area rectangle in histogram
    05:43 Calculating the area of rectangles with different heights and consecutive widths.
    07:44 Discussing how to find the largest area of a histogram by considering different heights and widths
    11:59 Brute force solution has O(n^2) complexity
    13:50 Using a stack to find left smaller boundary indexes
    17:30 Identifying left smaller elements and calculating boundaries in a histogram.
    19:15 Computing left and right smaller indexes using a stack
    22:45 Compute the largest rectangular area in histogram
    24:34 Using stack method to find left smaller and right smaller, leading to largest rectangular area.
    27:53 Using stack to find left and right smaller elements for histograms.
    29:27 Optimal solution explained using a 6-line code and single pass of left smaller to find maximum rectangle area

  • @sandeshkumarchandrakar1622

    just solved before video got released now this masterpiece comes thanks for efforts sir

  • @localhost_3000
    @localhost_3000 Před 2 lety +11

    Here is the complete optimized algorithm...
    1. create a stack st that will store indexes
    2. create two arrays left_smaller and right_smaller that will store leftmost smaller and rightmost smaller index for an index i respectively.
    // calculate left smaller...
    3. for every arr[i]:
    i. curr_height = arr[i]
    // stack_top_height means arr[st.top()]
    ii. while stack is not empty and stack_top_height >= curr_height:
    -> stack.pop()
    iii. if stack is empty left_smaller[i] = 0
    else left_smaller[i] = st.top() + 1
    iv. st.push(i);
    end for loop

    4. clear stack for reuse
    // calculate right smaller...this time traverse from the back..
    5. for every arr[i]
    i. curr_height = arr[i]
    ii. while stack is not empty and stack_top_height >= curr_height:
    stack.pop()
    iii. if stack is empty right_smaller[i] = n-1
    else right_smaller[i] = st.top() - 1
    iv. st.push(i)
    end for loop

    6. max_area = INT_MIN
    7. for every arr[i]
    i. curr_height = arr[i]
    ii. width = (right_smaller[i] - left_smaller[i] + 1)
    iii. max_area = max(max_area, curr_height * width)
    end for loop
    8. return max_area
    Any feedback will be appreciated

  • @DeepakKumar-yc9hr
    @DeepakKumar-yc9hr Před 3 lety +2

    you are a diamond ... no one can replace you bro ... .

  • @nehasingh8181
    @nehasingh8181 Před 2 měsíci

    Great job! You explain every concept so easily.....

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

    best. Liked the way to optimize the brute force. Other youtuber just directly jump on optimal solution.

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

    You are the best! Thanks for all the efforts :)

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

    Great explanation..learned the concept of next smaller/greater 🔥

  • @deepaksolanki1972
    @deepaksolanki1972 Před 2 lety

    Best Explanation, Hats off to the efforts. Subscribed !!!

  • @stith_pragya
    @stith_pragya Před 6 měsíci +2

    UNDERSTOOD.......Thank You So Much for this wonderful video...........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

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

    Amazing explanation man!! Coded the brute-force myself after just watching the 1/3rd of the video. You're doing god's work!!

  • @sunnykumarpal9679
    @sunnykumarpal9679 Před rokem

    Bhaiya you teaching is really fabulous .
    I enjoy learning from your videos.

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

    That T-shirt lol! But keep up with the good work, you explain shit very well!

  • @cinime
    @cinime Před 2 lety

    Amazing explanation, thank you very much!!

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

    The solution is well explained.
    Also, please add this shirt to your merchandise collection.

  • @AshishKumar-pq6pr
    @AshishKumar-pq6pr Před 3 lety +4

    Best explanation...maja aa gya 😅🤟

  • @h.kchannel4115
    @h.kchannel4115 Před rokem

    Thank you for this great explanation!!!

  • @Sneha-qz9ml
    @Sneha-qz9ml Před 2 lety

    The best explanation found!!..............

  • @anupam6045
    @anupam6045 Před 2 lety

    Thanks striver for the wonderful explanation

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

    Thank you much for making this video.

  • @prateekshrivastava2802
    @prateekshrivastava2802 Před 2 měsíci

    Striver The Real OG !! Period : )

  • @AKASHTHAKUR-rs4sg
    @AKASHTHAKUR-rs4sg Před 2 lety +15

    Happy Guru Purnima @StriverBhaiya, our Guru and Inspiration ... from the day I started watching your videos ... problem solving has became easy and damn interesting :),
    Your Vlogs and Lifestyle are really nice and for us a threshold...pushing us to give our best...
    Lots of Best Wishes And More Success to you...
    .
    .
    .
    .
    .
    Date: Tera Saath (13/7) hai to...hume kya kami hai...dsa v ab hume asan lag rahi h XD XD XD

  • @susrandomguy
    @susrandomguy Před rokem +2

    9:27 enjoy.
    btw great video as usual.

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

    crisp and clear!

  • @nonamejustpain4492
    @nonamejustpain4492 Před rokem +2

    Anyone who is not able to understand the solution....I would highly recommend aditya verma's solution ....THAT MAN IS A GOD

  • @hell-o8470
    @hell-o8470 Před 2 lety +3

    How to get logic of sub problems in mind ? The moment you told next greater element at 13:05 . I immediately got the idea how to solve and got AC in one go as I had solve next greater element problem. But "how do we get that sub concept at right time? "

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

    Can we do this in a single traversal?
    There are solutions in Leetcode Discuss with 1 traversal, but could not understand.

  • @devbhattacharya153
    @devbhattacharya153 Před rokem

    Best explanation video ever!!!

  • @hariomtiwari7605
    @hariomtiwari7605 Před rokem

    Superb Explanation Bhaiya 👌👌👌👌

  • @sourabhsisodia9563
    @sourabhsisodia9563 Před 3 lety

    striver bhai tum bhot mast kam krta hain....

  • @kalebakeitshokile1366

    what a great explanation!

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

    Superb explanation ....

  • @123_shreyoshide9
    @123_shreyoshide9 Před 2 lety

    GOD LEVEL! THANK YOU!

  • @teacup1431
    @teacup1431 Před 9 měsíci +2

    Can we do sorting and take product of each element and number of remaining elements . It can be done in one loop

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

    Best explanation!!!!

  • @ankitraibole7885
    @ankitraibole7885 Před 2 lety

    great explanation!

  • @akashskumar99
    @akashskumar99 Před 2 lety

    superb explanation bruh

  • @rydmerlin
    @rydmerlin Před rokem

    This assumes you have smaller on both left and right but you aren’t always going to expect that. You can have increasing from left to right or decreasing from left to right.

  • @suryansh70
    @suryansh70 Před 2 lety

    such a nice explaination

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

    Thanks for the detailed explanation!!

    • @shubhamrane2918
      @shubhamrane2918 Před 2 lety

      Is there a playlist of the leetcode solutions on your channel?

  • @vm1662
    @vm1662 Před 9 měsíci +1

    Thanks Striver for yet another amazing video!!!
    Just a question :
    Can another brute force approach be finding the area for each combination possible? Like so below:
    public int largestRectangleArea(int[] heights) {
    int maxArea = Integer.MIN_VALUE;
    for(int i = 0; i < heights.length; i++) {
    int min = heights[i];
    for(int j = i; j < heights.length; j++) {
    min = Math.min(min, heights[j]);
    int area = min * (j-i+1);
    maxArea = Math.max(area, maxArea);
    }
    }
    return maxArea;
    }

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

    understood, thanks

  • @abhinay.k
    @abhinay.k Před 6 dny

    thank you

  • @rebellious_703
    @rebellious_703 Před 2 lety

    Wah Arnab Wah ❤

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

    Nicee T-Shirtt!!!!!

  • @rydmerlin
    @rydmerlin Před rokem +1

    Why in the second example did you exclude the the first bar ? 1x5=5 why isn’t it 1x6?

  • @user-gq6sq8zk8s
    @user-gq6sq8zk8s Před 6 měsíci +1

    Thank You !!

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

    Brilliant thanks

  • @akshaydutt8162
    @akshaydutt8162 Před 3 lety

    Part 2 - czcams.com/video/jC_cWLy7jSI/video.html&ab_channel=takeUforward

  • @chitranshkumar6694
    @chitranshkumar6694 Před 3 lety

    Thanks bhaiya for making this video

  • @newbienate
    @newbienate Před rokem

    Rider doing good. Thanks

  • @surbhijain8800
    @surbhijain8800 Před 2 lety

    thank you very much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    indeed helpful

  • @ShubhamMahawar_Dancer_Actor

    The link you have provided for part2 is the link of Part 1 .reverify it.

  • @mihiragarwal1269
    @mihiragarwal1269 Před 2 lety

    loved the video

  • @yashkhatwani3198
    @yashkhatwani3198 Před rokem +1

    Amazing

  • @jeevan999able
    @jeevan999able Před rokem

    Man thank you !

  • @harshkumarsharma1169
    @harshkumarsharma1169 Před 3 měsíci +1

    18:21 Is this just me who listened to some other noise here

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

    understood!!

  • @anjaliyadav9360
    @anjaliyadav9360 Před rokem

    Thanks!

  • @dsa_tutorial
    @dsa_tutorial Před rokem

    we can also take 1 * 6 as 6 as height than also we will get rectangle whose area is maximum

  • @ananyaa8597
    @ananyaa8597 Před rokem

    Man this question is crazy

  • @COURATWENTYTHREE
    @COURATWENTYTHREE Před 8 dny

    understood❤❤❤

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

    understood

  • @rachanasingh2231
    @rachanasingh2231 Před 2 lety

    awesome!!

  • @prabhakaran5542
    @prabhakaran5542 Před měsícem +2

    Understood ❤❤❤ watching in 2024!

  • @jitengarg5740
    @jitengarg5740 Před 3 lety

    it's good that u try explain each and every part but u can make it more shorter it saves time
    after all ek do vaar dry run karne se samaj to a he jata hai

    • @takeUforward
      @takeUforward  Před 3 lety +11

      Actually point is sb k lie h video. Bht log aise bhi hote h jinhe pura chye detailed. So for people who don’t want detailed they can fast forward na. But if i trim, then the other guys will not understand

  • @subashkannan949
    @subashkannan949 Před rokem

    Indeed

  • @bahubaljain3063
    @bahubaljain3063 Před 3 lety

    Just doing the same question.....and video aagyi 😂😂😂 ye mehaz itedaaq h ya khuda ka ishara🌸

  • @KRajesh-ej7dy
    @KRajesh-ej7dy Před rokem

    Bro thanks for the video.I got total clarity after watching this.But the java code which u have given is not executing.

  • @samdarshikumar6488
    @samdarshikumar6488 Před 3 lety

    🔥🔥🔥🔥🔥🔥 best

  • @gaurikale6404
    @gaurikale6404 Před 3 lety

    Thanks a lot 🤞💯💯💯💯

  • @souravsarkar6107
    @souravsarkar6107 Před 2 lety

    understood sir

  • @abhisheksoni9644
    @abhisheksoni9644 Před 2 lety

    can we solve this using doubly linked list?

  • @AnujSingh-ju4nr
    @AnujSingh-ju4nr Před rokem +1

    bhai itna stress mat diya karo, over ho jata hai kya !
    heath ka dhyyan rakkho😄

  • @navyasri5077
    @navyasri5077 Před 2 lety

    height of instagram..lol .actually this indicates i am listening so carefully 😋

  • @gowthamarun43
    @gowthamarun43 Před 3 lety

    Super 💥💥

  • @jitendrasinghsola
    @jitendrasinghsola Před 2 lety

    the link in the description is pointing to the same video not part 2

  • @vivekpawar311
    @vivekpawar311 Před 2 lety

    Understood!

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

    Understood !!!!!

  • @lBaZeRl
    @lBaZeRl Před 7 měsíci

    Ain't no way nobody's talking about the fire shirt

  • @NARUTOUZUMAKI-bk4nx
    @NARUTOUZUMAKI-bk4nx Před 7 měsíci

    Understood

  • @user-ol8gr4dn1h
    @user-ol8gr4dn1h Před 3 měsíci

    best

  • @ayush.kumar.13907
    @ayush.kumar.13907 Před 3 lety

    nice tshirt

  • @susantakumardutta6283

    Understood🙂🙂

  • @yahoo-kids
    @yahoo-kids Před rokem

    oo bhaii maja aagya

  • @veeresh4441
    @veeresh4441 Před 3 lety

    what if there is some rectangle with zero height at middle? giving 0 when stack is empty wont work right?

    • @takeUforward
      @takeUforward  Před 3 lety

      Without taking that, we will consider the other rectangle..

    • @findingMyself.25yearsago
      @findingMyself.25yearsago Před rokem

      Veeresh, even though it takes some other left_min, right_min this particular rectangle won't be considered because (right-left) * 0 (Height). so always this would be 0 and won't be considered in the max space.. correct me if I'm wrong

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

    it's giving segmentation fault in gfg, Anyone else?
    Thanks in advance :)

  • @HarshGupta-wi1zn
    @HarshGupta-wi1zn Před 2 lety +1

    Getting runtime error segmentation fault error for the Same code in GFG.
    Expected time complexity : O(N)
    Expected Space complexity:O(N)

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

    actual video starts at 8:46

  • @sarthakkabra8156
    @sarthakkabra8156 Před rokem

    9:28 what was that bro🤣🤣🤣🤣

  • @haidarmohammad8510
    @haidarmohammad8510 Před 11 měsíci +1

    Why are you concern about greater element in the stack? Please reply..........

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

      because a bigger histogram / building / block is something ith element can expand into, but it cannot expand into a smaller one

  • @vuyyurishanmukhaabhinavvar4096

    Understood striver