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!
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
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
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
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.
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]
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👌👌👍👍❣
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
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.
Brute force code public static int largestRectangleAreaI(int[] heights) { int maxArea = 0; for (int i = 0; i < heights.length; i++) { int left = i; int right = i; while (left >= 0 && heights[left] >= heights[i]) { left--; } while (right < heights.length && heights[right] >= heights[i]) { right++; } maxArea = Math.max(maxArea, (right - left - 1) * heights[i]); } return maxArea; }
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.
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
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?
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
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 ++
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
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
Anuj bhiya #ये दिल मांगे मोर वीडियो on DSA
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
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 ..
this question is asked in my IBM exam but they want to know the largest square in a histogram. Thnaks bhaiya.
👍🏻👍🏻👍🏻
for O(n^2) approach, while condition should have = , at 8:34
waah bhai choti aur clickwait video ke liye function hi gayab kardiya, kya baat hai 👏👏👏👏👏👏👏
superb explanation
Bhai well explained.. 👍
But code in description is different one 🥲
concept may be similar to container with most water . But it is little different but analogy is same
very helpfull video anuj sir
Crystal clear explanation bhaiya ❣️❣️❣️
Amazing. Short and clear. Thanks a lot.
Ek baat to sach hai bhaiya ki aap teaching me sabhse badhiiya ho
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
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
I just love your teaching skils
and I love your writing skills
@@alexrcrew1975 and i love your typing skills
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.
thank you sir for this amazing solution
very nice explanation
Thank sir 👍 for your superb explanation
YOU ARE GREAT SIRJI
Thank you 😊😊😊💚
At 11:56 there is typo. In previous smaller method line no 10 it should be ps[i]=-1, not ns[i]=-1
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
superb
you teach very nicely
Thank u sir🔥🔥🔥
GREAT EXPLANATION
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👌👌👍👍❣
ekdam Jhakaash Dada !!
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..😂😂
it's been 3 years, did you get into Microsoft Linkedin?
Very well explained. Thanks
sir please make a video on ""how to learn Javascript""
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
Great Video!
thanku so muchh😀
why will the logic of previous smaller and next smaller work? at 6:37
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.
great!!! really helpful
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
well explained.
Very famous question thank you sir ❤
Thank you Anuj Bhaiya!
You are a Gem! 😀
thankyou sirrr ,plzz more questions practice
Thank You
Bhaiya,I was able to find same logic like yours but it took me around 20 mins.
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 .
thanks bro
its not working on leetcode
it is working properly
WOW bro ty for video
GFG ka link galat Question ka hai,pls correct
Hi Anuj Bhaiya, Thanks for all your videos they've been helping a lot..
bhaiya ye index (right-left-1) kyu kiya hai
why we subtract 1 when we calculate the width??
Hi bhaiya , please start javascript video tutorials series
Sir please help me to prepare for amazon.
Where can we get this code's link?
How that formula came from and why we are using next smaller and previous smaller... Can someone tell me?
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 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];
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
Video on AI/ML Projects bhayia
Great explanation bhaiya, but this approach is still very slow on leetcode
EDIT: IT BECOMES 14
12th comment But I want heart
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.
This is asked in an interview 😂❤
stack mai hamne values ki jagah index kyu store kare hai ?
code for this?? plz share link
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!
Brute force code
public static int largestRectangleAreaI(int[] heights) {
int maxArea = 0;
for (int i = 0; i < heights.length; i++) {
int left = i;
int right = i;
while (left >= 0 && heights[left] >= heights[i]) {
left--;
}
while (right < heights.length && heights[right] >= heights[i]) {
right++;
}
maxArea = Math.max(maxArea, (right - left - 1) * heights[i]);
}
return maxArea;
}
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..
Important question 👍 thanku... Sir
Ans ans is wrong for test case [9,0]
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
sir i'm diploma pass out student .how to prapare job in MNC company
Why a-b-1 ????
Intuitive kaise hai ye
Hello sir, how we define function nextSmaller(a)??
This function is defined in the previous video of this tutorial series.
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.
❤️❤️🙏🙏
Hi
Coding
c++ code anyone pls?
samjhane se zyaada show-off krne me interest hai
kuch aisa bataunga jo sunke hairan ho jaoge sab log pls batao
Bhaii
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
great explanation