Largest Rectangular in Histogram | Maximum Rectangular Area in a Histogram | DSA-One Course #44
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
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!
ya you are correct but how this solution will be expected because in question constraint is 1
One of the easiest explaination people literally took an hour to explain such easy concept. Great Anuj Bhaiya respect ++
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
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
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
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 ..
Bhai well explained.. 👍
But code in description is different one 🥲
this question is asked in my IBM exam but they want to know the largest square in a histogram. Thnaks bhaiya.
👍🏻👍🏻👍🏻
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
Please make a 60day roadmap for DSA beginner to crack Microsoft linkedin level companies
Are you really sure bro...u can learn total dsa in 30 days
Nice joke 🤣🤣
In 60 days you can learn just basics of dsa..😂😂
concept may be similar to container with most water . But it is little different but analogy is same
This code not give right answer with other inputs.
Everyone try it first. Wrong code
if(ns[i] == -1) ns =n; // add this condition
int cur = (ns[i] - ps[i] - 1) * a[i];
Crystal clear explanation bhaiya ❣️❣️❣️
Amazing. Short and clear. Thanks a lot.
Anuj bhiya #ये दिल मांगे मोर वीडियो on DSA
very helpfull video anuj sir
for O(n^2) approach, while condition should have = , at 8:34
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]
thanks for the soln
But how to print next smaller element
Thanks a lot for the solution.
thank you bhai
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👌👌👍👍❣
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.
superb explanation
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.
Thank sir 👍 for your superb explanation
its not working on leetcode
it is working properly
very nice explanation
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
thank you sir for this amazing solution
sir please make a video on ""how to learn Javascript""
I just love your teaching skils
and I love your writing skills
@@alexrcrew1975 and i love your typing skills
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 .
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
YOU ARE GREAT SIRJI
At 11:56 there is typo. In previous smaller method line no 10 it should be ps[i]=-1, not ns[i]=-1
Thank you 😊😊😊💚
Ek baat to sach hai bhaiya ki aap teaching me sabhse badhiiya ho
superb
Very well explained. Thanks
Thank u sir🔥🔥🔥
Very famous question thank you sir ❤
you teach very nicely
thankyou sirrr ,plzz more questions practice
GREAT EXPLANATION
I do not understand why we have taken -1 in the brute force solution area = (right - left - 1) *a[i] please sir help.
@rahulrai it should be +1 .. he made a mistake
How that formula came from and why we are using next smaller and previous smaller... Can someone tell me?
Thank you Anuj Bhaiya!
You are a Gem! 😀
Bhaiya,I was able to find same logic like yours but it took me around 20 mins.
well explained.
Thank You
great!!! really helpful
Hi Anuj Bhaiya, Thanks for all your videos they've been helping a lot..
Can someone please explain me, why we are doing ps[i] -1 while calculating the area.?
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.
why we subtract 1 when we calculate the width??
Great Video!
why will the logic of previous smaller and next smaller work? at 6:37
bhaiya ye index (right-left-1) kyu kiya hai
GFG ka link galat Question ka hai,pls correct
thanks bro
ekdam Jhakaash Dada !!
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?
we are not accessing index 4
thanku so muchh😀
Sir please help me to prepare for amazon.
Hi bhaiya , please start javascript video tutorials series
getting index out of bound how can we get 9th index if array size is less than that(nextsmallest method)
Write arr.length if nextSmaller not present
This is asked in an interview 😂❤
code for this?? plz share link
WOW bro ty for video
Great explanation bhaiya, but this approach is still very slow on leetcode
EDIT: IT BECOMES 14
12th comment But I want heart
Where can we get this code's link?
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.
Only desktop
please write code side by side when u explain the logic of problem..
stack mai hamne values ki jagah index kyu store kare hai ?
Great Explanation as always !!
In your code , in below snippet did you mean ps[i] = -1 ?
"if(s.isEmpty()){
ns[i] = -1;
}
ps hai wo. galti se likhliya hoga!
Important question 👍 thanku... Sir
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
stack.peek is written inside a[] so it's element only
see the code once again
Video on AI/ML Projects bhayia
Ans ans is wrong for test case [9,0]
sir i'm diploma pass out student .how to prapare job in MNC company
Hello sir, how we define function nextSmaller(a)??
This function is defined in the previous video of this tutorial series.
Intuitive kaise hai ye
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?
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.
Why a-b-1 ????
Hi
❤️❤️🙏🙏
c++ code anyone pls?
Coding
Bhaii
kuch aisa bataunga jo sunke hairan ho jaoge sab log pls batao
samjhane se zyaada show-off krne me interest hai
are u iitian
Nsut
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
Bhai iska koi video solution bhi h kya?
bhaiya app bhagwan m believe karte ho ki nahi pls batao na pls pls pls sab log batao na please