Largest Rectangle in Histogram | Part - 1 | Leetcode 84

  Рет қаралды 223,149

take U forward

take U forward

Күн бұрын

Пікірлер: 156
@divyagupta6854
@divyagupta6854 2 жыл бұрын
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 2 жыл бұрын
yes, I am just bothered with too much to remember - formula and technique :(
@manojnavinjamuri5867
@manojnavinjamuri5867 Жыл бұрын
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.
@nonamenoname2801-xj5
@nonamenoname2801-xj5 Жыл бұрын
Anyone who is not able to understand the solution....I would highly recommend aditya verma's solution ....THAT MAN IS A GOD
@stutisuchak4525
@stutisuchak4525 2 жыл бұрын
I saw multiple videos on this question, but this one made it clear. Thank you so much.
@localhost_3000
@localhost_3000 2 жыл бұрын
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
@sahityavatturi121
@sahityavatturi121 2 жыл бұрын
👏
@nihalsingh6233
@nihalsingh6233 9 ай бұрын
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
@mukeshmahajan1586
@mukeshmahajan1586 3 жыл бұрын
bhai the energy with which u explain incredibly amazing!
@thisisankitgaur
@thisisankitgaur 3 жыл бұрын
❤️ from IIT Bombay. Nice Work!!
@trojanhorse8278
@trojanhorse8278 Жыл бұрын
Hum log Mtech wale hai , hame dekhna padta hai :}
@Aditya-m4r6v
@Aditya-m4r6v Жыл бұрын
Or bhai kaha lgi placement
@phantomhawk489
@phantomhawk489 11 ай бұрын
​@@Aditya-m4r6v bhai usse shadi karoge kya?
@MPrachiShinde
@MPrachiShinde 7 ай бұрын
Flex karne ka tareeka thoda kezual hai
@ankitsablok952
@ankitsablok952 2 жыл бұрын
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.
@takeUforward
@takeUforward 2 жыл бұрын
There is a part-2 as well
@akshyagarg2398
@akshyagarg2398 3 жыл бұрын
best. Liked the way to optimize the brute force. Other youtuber just directly jump on optimal solution.
@anshuljain1700
@anshuljain1700 3 жыл бұрын
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👍❤️
@DeepakKumar-yc9hr
@DeepakKumar-yc9hr 3 жыл бұрын
you are a diamond ... no one can replace you bro ... .
@stith_pragya
@stith_pragya 10 ай бұрын
UNDERSTOOD.......Thank You So Much for this wonderful video...........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@susrandomguy
@susrandomguy 2 жыл бұрын
9:27 enjoy. btw great video as usual.
@prabhakaran5542
@prabhakaran5542 4 ай бұрын
Understood ❤❤❤ watching in 2024!
@prabhakaran5542
@prabhakaran5542 4 ай бұрын
July 1st
@sandeshkumarchandrakar1622
@sandeshkumarchandrakar1622 3 жыл бұрын
just solved before video got released now this masterpiece comes thanks for efforts sir
@narendrakumariitb
@narendrakumariitb 2 жыл бұрын
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
@yashkumardhawan6961
@yashkumardhawan6961 Жыл бұрын
The solution is well explained. Also, please add this shirt to your merchandise collection.
@nehasingh8181
@nehasingh8181 6 ай бұрын
Great job! You explain every concept so easily.....
@sunnykumarpal9679
@sunnykumarpal9679 2 жыл бұрын
Bhaiya you teaching is really fabulous . I enjoy learning from your videos.
@prateekshrivastava2802
@prateekshrivastava2802 6 ай бұрын
Striver The Real OG !! Period : )
@AshishKumar-pq6pr
@AshishKumar-pq6pr 3 жыл бұрын
Best explanation...maja aa gya 😅🤟
@hell-o8470
@hell-o8470 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? "
@rydmerlin
@rydmerlin Жыл бұрын
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.
@teacup1431
@teacup1431 Жыл бұрын
Can we do sorting and take product of each element and number of remaining elements . It can be done in one loop
@sourabhsisodia9563
@sourabhsisodia9563 3 жыл бұрын
striver bhai tum bhot mast kam krta hain....
@harshkumarsharma1169
@harshkumarsharma1169 7 ай бұрын
18:21 Is this just me who listened to some other noise here
@hariomtiwari7605
@hariomtiwari7605 Жыл бұрын
Superb Explanation Bhaiya 👌👌👌👌
@rebellious_703
@rebellious_703 2 жыл бұрын
Wah Arnab Wah ❤
@Sneha-qz9ml
@Sneha-qz9ml 3 жыл бұрын
The best explanation found!!..............
@democratcobra
@democratcobra 2 жыл бұрын
Superb explanation ....
@yashwagh83
@yashwagh83 3 жыл бұрын
That T-shirt lol! But keep up with the good work, you explain shit very well!
@ShubhamMahawar_Dancer_Actor
@ShubhamMahawar_Dancer_Actor 3 жыл бұрын
The link you have provided for part2 is the link of Part 1 .reverify it.
@sagarlandge3271
@sagarlandge3271 2 жыл бұрын
crisp and clear!
@studyaccount794
@studyaccount794 2 жыл бұрын
Amazing explanation man!! Coded the brute-force myself after just watching the 1/3rd of the video. You're doing god's work!!
@AKASHTHAKUR-rs4sg
@AKASHTHAKUR-rs4sg 2 жыл бұрын
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
@nileshsinha7869
@nileshsinha7869 3 жыл бұрын
Great explanation..learned the concept of next smaller/greater 🔥
@devbhattacharya153
@devbhattacharya153 2 жыл бұрын
Best explanation video ever!!!
@vm1662
@vm1662 Жыл бұрын
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; }
@deepaksolanki1972
@deepaksolanki1972 2 жыл бұрын
Best Explanation, Hats off to the efforts. Subscribed !!!
@sabbysays
@sabbysays 3 жыл бұрын
You are the best! Thanks for all the efforts :)
@tusharjolly2734
@tusharjolly2734 2 жыл бұрын
Best explanation!!!!
@jitengarg5740
@jitengarg5740 3 жыл бұрын
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 3 жыл бұрын
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
@HarshGupta-wi1zn
@HarshGupta-wi1zn 3 жыл бұрын
Getting runtime error segmentation fault error for the Same code in GFG. Expected time complexity : O(N) Expected Space complexity:O(N)
@004-u6f
@004-u6f Жыл бұрын
same ,any idea how to correct it?
@anupam6045
@anupam6045 2 жыл бұрын
Thanks striver for the wonderful explanation
@cinime
@cinime 2 жыл бұрын
Amazing explanation, thank you very much!!
@alamatel
@alamatel 3 жыл бұрын
Thank you much for making this video.
@akashskumar99
@akashskumar99 3 жыл бұрын
superb explanation bruh
@bahubaljain3063
@bahubaljain3063 3 жыл бұрын
Just doing the same question.....and video aagyi 😂😂😂 ye mehaz itedaaq h ya khuda ka ishara🌸
@dsa_tutorial
@dsa_tutorial Жыл бұрын
we can also take 1 * 6 as 6 as height than also we will get rectangle whose area is maximum
@h.kchannel4115
@h.kchannel4115 2 жыл бұрын
Thank you for this great explanation!!!
@gopropro3544
@gopropro3544 3 жыл бұрын
Nicee T-Shirtt!!!!!
@newbienate
@newbienate 2 жыл бұрын
Rider doing good. Thanks
@rydmerlin
@rydmerlin Жыл бұрын
Why in the second example did you exclude the the first bar ? 1x5=5 why isn’t it 1x6?
@dataman4503
@dataman4503 3 жыл бұрын
Can we do this in a single traversal? There are solutions in Leetcode Discuss with 1 traversal, but could not understand.
@yashkhatwani3198
@yashkhatwani3198 Жыл бұрын
Amazing
@KRajesh-ej7dy
@KRajesh-ej7dy Жыл бұрын
Bro thanks for the video.I got total clarity after watching this.But the java code which u have given is not executing.
@ananyaa8597
@ananyaa8597 2 жыл бұрын
Man this question is crazy
@StudyEmail-p9u
@StudyEmail-p9u 10 ай бұрын
Thank You !!
@suryansh70
@suryansh70 2 жыл бұрын
such a nice explaination
@lBaZeRl
@lBaZeRl 10 ай бұрын
Ain't no way nobody's talking about the fire shirt
@ashishpradhan6250
@ashishpradhan6250 4 ай бұрын
understood, thanks
@ankitr2969
@ankitr2969 2 жыл бұрын
great explanation!
@123_shreyoshide9
@123_shreyoshide9 2 жыл бұрын
GOD LEVEL! THANK YOU!
@kalebakeitshokile1366
@kalebakeitshokile1366 2 жыл бұрын
what a great explanation!
@AnujSingh-ju4nr
@AnujSingh-ju4nr 2 жыл бұрын
bhai itna stress mat diya karo, over ho jata hai kya ! heath ka dhyyan rakkho😄
@aj9706
@aj9706 3 жыл бұрын
Brilliant thanks
@abhinay.k
@abhinay.k 3 ай бұрын
thank you
@shubhamrane2918
@shubhamrane2918 2 жыл бұрын
Thanks for the detailed explanation!!
@shubhamrane2918
@shubhamrane2918 2 жыл бұрын
Is there a playlist of the leetcode solutions on your channel?
@haidarmohammad8510
@haidarmohammad8510 Жыл бұрын
Why are you concern about greater element in the stack? Please reply..........
@OrbitZyro
@OrbitZyro 8 ай бұрын
because a bigger histogram / building / block is something ith element can expand into, but it cannot expand into a smaller one
@dadidivya8663
@dadidivya8663 2 жыл бұрын
Python Sol: class Solution: def largestRectangleArea(self, heights: List[int]) -> int: n = len(heights) li = [0 for i in range(n)] ri = [n-1 for i in range(n)] s1 = [0] s2 = [n-1] maxi = 0 for i in range(1,n): while(s1!=[] and heights[i]
@satyampande684
@satyampande684 2 жыл бұрын
understood!!
@aks-nc6kq
@aks-nc6kq 2 жыл бұрын
Bro you look like munawar faruqui
@chitranshkumar6694
@chitranshkumar6694 3 жыл бұрын
Thanks bhaiya for making this video
@yahoo-kids
@yahoo-kids Жыл бұрын
oo bhaii maja aagya
@souravsarkar6107
@souravsarkar6107 2 жыл бұрын
understood sir
@shwetanksingh5208
@shwetanksingh5208 2 жыл бұрын
//Brute force solution class Solution { public: int largestRectangleArea(vector& heights) { int ans = 0,n=heights.size(); for(int i=0;i=0 && heights[leftSmaller]>=heights[i]); ans = max(ans,(rightSmaller-leftSmaller-1)*heights[i]); } return ans; } };
@zeeshanhasankhan
@zeeshanhasankhan 2 жыл бұрын
ans = max(ans,(rightSmaller-leftSmaller+1)*heights[i]);
@subashkannan949
@subashkannan949 Жыл бұрын
Indeed
@jitendrasinghsola
@jitendrasinghsola 2 жыл бұрын
the link in the description is pointing to the same video not part 2
@mihiragarwal1269
@mihiragarwal1269 2 жыл бұрын
loved the video
@navyasri5077
@navyasri5077 2 жыл бұрын
height of instagram..lol .actually this indicates i am listening so carefully 😋
@anjaliyadav9360
@anjaliyadav9360 2 жыл бұрын
Thanks!
@dreamyme543
@dreamyme543 2 жыл бұрын
Understood
@harshitjaiswal9439
@harshitjaiswal9439 8 ай бұрын
understood
@jeevan999able
@jeevan999able Жыл бұрын
Man thank you !
@rubyshorts281
@rubyshorts281 9 ай бұрын
actual video starts at 8:46
@susantakumardutta6283
@susantakumardutta6283 2 жыл бұрын
Understood🙂🙂
@COURATWENTYTHREE
@COURATWENTYTHREE 3 ай бұрын
understood❤❤❤
@vivekpawar311
@vivekpawar311 2 жыл бұрын
Understood!
@surbhijain8800
@surbhijain8800 3 жыл бұрын
thank you very much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! indeed helpful
@abhisheksoni9644
@abhisheksoni9644 2 жыл бұрын
can we solve this using doubly linked list?
@ayush.kumar.13907
@ayush.kumar.13907 3 жыл бұрын
nice tshirt
@khushil9988
@khushil9988 7 ай бұрын
take a bow
@rachanasingh2231
@rachanasingh2231 2 жыл бұрын
awesome!!
@gowthamarun43
@gowthamarun43 3 жыл бұрын
Super 💥💥
@kyakya6490
@kyakya6490 2 жыл бұрын
Liked it due to the tshirt lmao
@srinathv1412
@srinathv1412 9 ай бұрын
Understood !!!!!
@samdarshikumar6488
@samdarshikumar6488 3 жыл бұрын
🔥🔥🔥🔥🔥🔥 best
@prashantbisht2219
@prashantbisht2219 3 жыл бұрын
Shouldn't it be O(n^2), since consider an non increasing order array, for right side, we will delete every element greater than current, hence for every element we have to traverse till very first, shouldn't that be n^2 ???????????
@takeUforward
@takeUforward 3 жыл бұрын
Every element is pushed once and popped out once. So ideally its 2n
@prashantbisht2219
@prashantbisht2219 3 жыл бұрын
@@takeUforward Yaaa I got it now, thanks dude, you even replied in your old videos, that's great..
@khushil9988
@khushil9988 7 ай бұрын
why i am feeling like this is similar to trapping rain water
@aryanagarwal7903
@aryanagarwal7903 7 ай бұрын
Concept is same
Largest Rectangle in Histogram | Part - 2 (6-7 lines of Code Approach)
19:18
Sliding Window Maximum | Leetcode
20:16
take U forward
Рет қаралды 262 М.
Hoodie gets wicked makeover! 😲
00:47
Justin Flom
Рет қаралды 133 МЛН
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 8 МЛН
Disrespect or Respect 💔❤️
00:27
Thiago Productions
Рет қаралды 43 МЛН
L12. Largest Rectangle in Histogram | Stack and Queue Playlist
31:42
take U forward
Рет қаралды 43 М.
Imlement LFU Cache | Leetcode(Hard)
19:27
take U forward
Рет қаралды 127 М.
Next Greater Element | Two Variants | Leetcode
22:00
take U forward
Рет қаралды 257 М.
LARGEST RECTANGLE IN HISTOGRAM - Leetcode 84 - Python
17:20
NeetCode
Рет қаралды 246 М.
Coding Interview Problem: Largest Rectangle in a Histogram
16:18
Jackson Gabbard
Рет қаралды 309 М.
Maximum Product Subarray - Best Intuitive Approach Discussed
20:27
take U forward
Рет қаралды 232 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 300 М.
How to Get a Developer Job - Even in This Economy [Full Course]
3:59:46
freeCodeCamp.org
Рет қаралды 3,2 МЛН
Hoodie gets wicked makeover! 😲
00:47
Justin Flom
Рет қаралды 133 МЛН