Implement Min Stack | O(2N) and O(N) Space Complexity

  Рет қаралды 92,440

take U forward

take U forward

Күн бұрын

Пікірлер: 95
@takeUforward
@takeUforward 3 жыл бұрын
Please like the video, helps a lot :) Also please spare a second to drop in a comment if you understood or not ..
@shwetanknaveen
@shwetanknaveen 3 жыл бұрын
Hey brother....int take 4bytes and long takes 8bytes.....so it is not the case that your second approach saves space.....I have verified it by submitting both solution on leetcode too......following is the code for first approach class MinStack { public: stack stac;// MinStack() { } void push(int val) { if(stac.empty()) { stac.push(make_pair(val,val));//if stack is empty then the value being pushed will also be minimum itself } else { stac.push(make_pair(val,min(val,stac.top().second)));//minimum value will be minimum of minimum till here and this value } } void pop() { stac.pop(); } int top() { return stac.top().first; } int getMin() { return stac.top().second; } };
@shra1
@shra1 Жыл бұрын
In line no. 34, can we not reduce equation to "mini = elem;" (algebraically) ?
@artofwrick
@artofwrick 11 ай бұрын
The question had become medium in leetcode. Ahahahaha😂
@muhammedimdaad
@muhammedimdaad 2 жыл бұрын
amazing, but the question that I am having is, how in the world one can come across the idea for O(N)? like, that's tough to just think and come at the first place !!
@abhishek--absh
@abhishek--absh 10 ай бұрын
Yeah these kind of questions are hard to think of during the interview if you haven't done these before.
@atharvacodes3705
@atharvacodes3705 3 жыл бұрын
Finally understood after watching 2 times and dry running. Keep up the good work! :)
@shuvbhowmickbestin
@shuvbhowmickbestin Жыл бұрын
the level shouldn't be easy when asked to implement the solution at O(1) extra space. It should rather be medium/hard.
@tulika2863
@tulika2863 3 жыл бұрын
Thank you , thank you so much for putting your efforts and this kind of high quality content . 🙏🏽🥺
@ytg6663
@ytg6663 3 жыл бұрын
🥺🥺
@joshipratik232
@joshipratik232 7 ай бұрын
Here is simple logic. //Time Complexity for each function is O(1). class MinStack { public: pair st[30000]; int t,min; MinStack() { t=-1; min=INT_MAX; } void push(int val) { if(t==-1 || val
@rupamrai9455
@rupamrai9455 3 жыл бұрын
657 views and only 74 likes please each and everyone like and share this Precious content. Thanks, striver for your time and effort
@shivalikagupta3433
@shivalikagupta3433 2 жыл бұрын
Thank youu sooo much !!! this ques came in one of my friend's interview, mine is near and here I am clearing the concepts.
@madhu_mohanreddy
@madhu_mohanreddy 6 ай бұрын
and?
@rutuja7293
@rutuja7293 3 жыл бұрын
Amazing and fruitful as always.Thank you for your efforts.
@deepanshudahiya8966
@deepanshudahiya8966 6 ай бұрын
almost did the question but the pair thing wasnt able to think abouti it and when striver started he just made me clear about the concept
@vaalarivan_p
@vaalarivan_p Жыл бұрын
but doesnt using a stack consume as much memory as using stack since the former is 64 bits?
@bhavyramani4456
@bhavyramani4456 7 ай бұрын
you seriously think that pair is O(2*n) and long long int is O(n) than it does not make sense logically, because long long occupies twice memory than normal int, and if contraints were from int64_min, int64_max than you can never use second approach, so you can not say that 2nd approach is better than first one. Still 2nd approcah was very nice i am not completely against it.
@artofwrick
@artofwrick 11 ай бұрын
For leetcode, the answer wouldn't come at edge if you multiply with 2. Instead use add like min=min-st.peek()+min;
@rohan8758
@rohan8758 5 ай бұрын
Unbelievable explanation for modified value to be pushed into the stack & rollBack to prevMinimal value! Hey Striver How much good you are in mathematics at your school time or have you studied higher algorithmic mathematics like usually Famour algorithian has gone through that content once in their life.
@shantanugupta-oz1dx
@shantanugupta-oz1dx 6 ай бұрын
Thank you so much for explaining all the problems so well. Very helpful thank you so much again
@producer_6_1_musics13
@producer_6_1_musics13 5 ай бұрын
encoded_value=2 * new_minimum - old_minimum old_minimum=2 * current_minimum - encoded_value...this generalization helped me a lot,
@sayanmukherjee8563
@sayanmukherjee8563 3 жыл бұрын
Thank you so much for the great, simple and clear explanation.
@manojnavinjamuri5867
@manojnavinjamuri5867 Жыл бұрын
Thank you soo much. This helped me to think the solution in different perspective.
@aashishmittal79
@aashishmittal79 Жыл бұрын
Isn't the optimal solution O(2N) space as well because we are using long long which is double the size of int datatype?
@artofwrick
@artofwrick 11 ай бұрын
Yess. Infact using pair class is safe
@stith_pragya
@stith_pragya 8 ай бұрын
Understood.............Thank You So Much for this wonderful video..........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@manaswitadatta
@manaswitadatta 3 жыл бұрын
Finally understood the proof clearly. Ty :)
@aditi1729
@aditi1729 Жыл бұрын
why are we storing modified value in the stack?
@vasujain1970
@vasujain1970 3 жыл бұрын
Great video as usual brother, enjoy your content a lot! Keep up the good work!
@tivialvarez
@tivialvarez Жыл бұрын
How were you able to come up with a formula that always ensured the result would be smaller than the minimum value? I can understand solutions like this but I don't understand how you were able to find such patterns sometimes. Were there any mathematical rules you used? I know that one important part was that you included the previous minimum in the calculation so that you are able to derive it from the stack value later when popping. How did you even get the idea to encode the previous minimum into the stack value to begin with? I find these kinds of discussions much more interesting and helpful to learners than just simply showing solutions, although its appreciated.
@tivialvarez
@tivialvarez Жыл бұрын
I guess one kind of intuition that could be used is: if we want to store extra information/data without using extra memory, we should look for places in the existing memory we're using to sneak in some extra information. For example assuming that all JavaScript numbers are 64-bits and our range of numbers is small, then we'll never use or overflow the full 64 bits (although it is something to be wary of.) So in some of those unused bits we can store that extra data as long as we can consistently undo and redo whatever we did to regain the original value. This also only works because a manipulation of the stack value means the min value has changed, and the only time the min value changes is when the stack value is manipulated. Therefore, whenever the stack value is not what we expect it to be, the min value has to be the value that the stack was "supposed" to be. That value is also necessary to get back the value of the previous minimum. I'd still love to hear the thought process on how you came up with a "function" that reliably returned a value smaller than the min value for that comparison though.
@artofwrick
@artofwrick 11 ай бұрын
Arithmetic Progression
@Priyankayadav-zt8ck
@Priyankayadav-zt8ck Ай бұрын
Why 2* val -min ? how you are deciding 2* val will work and 3*val or any other num*val will not work ? Please explain..
@geekyNib
@geekyNib 11 ай бұрын
Hey Striver ..I didn't get how this formula is derived ..like (2*current_min- earlier_min) while pushing..can't we use someother...?? Like not able to intuit it for O(2*N)->O(N)...
@artofwrick
@artofwrick 11 ай бұрын
min=min-st.peek()+min
@albedo9617
@albedo9617 6 ай бұрын
If we add -2 to the stack, pop it and then try to access the value of minimum, it will return as 2, but shouldn't it return us 0 or nothing in this case?
@ashokdurunde1814
@ashokdurunde1814 2 жыл бұрын
Thank you , thank you so much for putting your efforts and this kind of high quality content . 🙏🏽
@MindsetMotivation75
@MindsetMotivation75 2 жыл бұрын
In O(2N) 2 is just a constant . So , why we are considering this ?
@akash_assist
@akash_assist Жыл бұрын
because interviewer will ask you to optimize your code more and more......and you can't say no to the interviewer......if you say so.....he will just mark "not solved"......and you will get yourself rejected.
@tivialvarez
@tivialvarez Жыл бұрын
Constants rarely make a noticeable difference but they can sometimes. It's always just good to know anyways. If you have an O(N) vs an O(40N) solution with no other real difference why wouldn't you always prefer the O(N) solution? It may not be significantly or even noticeably faster, but it is in fact faster. Maybe even less complex.
@Musicuvakavi1823
@Musicuvakavi1823 8 ай бұрын
Great teacher in the world
@naikajsevak6090
@naikajsevak6090 Жыл бұрын
can we do like this if we get val as min of minSoFar then we multiply the both number and store it modify value in stack and atlast whenever we the pop operation come we check if the minSofar is not equal to top of value of stack then we divide min value and get previous min am i right wrong please correct me if i have wrong
@AngadSingh97
@AngadSingh97 2 жыл бұрын
I am now having a great time solving problems everyday, wow
@Neil.Menezes
@Neil.Menezes 16 күн бұрын
We're talking about optimizing O(2N) to O(N) but now instead of using a stack of a pair of 32 bit integers, we're using 64 bit longs so it is still taking 2x space 😄
@ridj41
@ridj41 11 ай бұрын
2nd approach wasn't intuitutive at all.
@siddharth794
@siddharth794 2 жыл бұрын
This videos should be in stack playlist
@techbus6016
@techbus6016 10 ай бұрын
If you are using long instead of int, you are already utilising double the space of elements, it's like instead of storing min separately in a int variable mini, you are storing same information in a long var. you are just widening the data type instead of using separate var, both solution will use the same space.There is no such optimization.
@aakritisharma5220
@aakritisharma5220 2 жыл бұрын
best teacher over youtube
@freshcontent3729
@freshcontent3729 3 жыл бұрын
bhaiya placements are starting, please upload many videos, go on full speed uploading ! :)
@sukhii0220
@sukhii0220 6 ай бұрын
Is no one concerned about it ? why we use formula in push opr 2*val-mini ? why we come with this ? please explain
@aashayagarwal8903
@aashayagarwal8903 Жыл бұрын
why are we storing the modified value?? just store the original and take min?
@gyaniyoda4608
@gyaniyoda4608 Жыл бұрын
Time complexity will be O(n)
@artofwrick
@artofwrick 11 ай бұрын
Cuz... You'll lose track of which was the last min and which was even last everytime you pop...
@risingengineers
@risingengineers 3 жыл бұрын
i wish i can meet you one day bhaia.
@SaiTeja-ob6zg
@SaiTeja-ob6zg 3 жыл бұрын
wonderful explaination brother!
@techwithindrani9376
@techwithindrani9376 2 жыл бұрын
Great content, thanks much for your effort. 😊
@scorcismweb5723
@scorcismweb5723 Жыл бұрын
2nd one is easy but im sure i wont be able to recall this
@Brute_Coder
@Brute_Coder Жыл бұрын
Here is the implementation of first method (sc -> O(2N) ) using Pair class in java.... // create a Pair class to store data and the min value encountered till then class Pair { int data; int min; public Pair(int _data, int _min) { this.data = _data; this.min = _min; } } class MinStack { Stack < Pair > stack; int minValue; public MinStack() { stack = new Stack < > (); minValue = Integer.MAX_VALUE; } public void push(int val) { if (val < minValue) minValue = val; // check if the value is lowest or not stack.push(new Pair(val, minValue)); } public void pop() { stack.pop(); if (!stack.isEmpty()) { minValue = stack.peek().min; } else { minValue = Integer.MAX_VALUE; // If the stack is empty, reset minValue to its initial value. } } public int top() { return stack.peek().data; } public int getMin() { return stack.peek().min; } }
@Himanshu_Mahuri
@Himanshu_Mahuri 3 жыл бұрын
why you have not added the link of this video in sde sheet .bhaia give some time and please update links of sde sheet doc . it will be easier .
@Karan-vq5vg
@Karan-vq5vg 3 жыл бұрын
please update the link in the website
@adishijha8365
@adishijha8365 3 жыл бұрын
Amazing explanation!
@AmitSingh-ut4wt
@AmitSingh-ut4wt 2 жыл бұрын
Really amazing approach
@sanskaarpatni9137
@sanskaarpatni9137 3 жыл бұрын
Amazing as always!
@Rahul_246a
@Rahul_246a 2 жыл бұрын
Wonderfully explained
@_-6912
@_-6912 3 жыл бұрын
I understood the solution but I guess link is not reflecting in SDE sheet!
@takeUforward
@takeUforward 3 жыл бұрын
Forgot to add, will add it soon. Thanks.
@_-6912
@_-6912 3 жыл бұрын
@@takeUforward Thanks Raj 😊
@AshishKumar-pq6pr
@AshishKumar-pq6pr 3 жыл бұрын
Awesome lecture
@saouli1632
@saouli1632 11 ай бұрын
thank you bhiya❤🙏
@vaalarivan_p
@vaalarivan_p Жыл бұрын
5:15 - 7:55
@cypher7536
@cypher7536 2 жыл бұрын
Understood! 🔥
@BECSAQUIB
@BECSAQUIB Жыл бұрын
You did not tell the intution behind the logic. You just verified it.
@SuperWhatusername
@SuperWhatusername 2 жыл бұрын
understood Thanks
@Learnprogramming-q7f
@Learnprogramming-q7f 10 ай бұрын
Thank you Bhaiya
@rishabh6210
@rishabh6210 7 ай бұрын
how is it better if we end up taking long long T_T
@codeman3828
@codeman3828 10 ай бұрын
Crazy optimal solution
@nikhil_squats
@nikhil_squats 3 жыл бұрын
Please make tree series
@stalker1164
@stalker1164 3 жыл бұрын
He is saying this 102th time that Tree series comes after 100k subscribers
@sanskarrawat1047
@sanskarrawat1047 3 жыл бұрын
indeed indeed indeed...
@yushsingla9905
@yushsingla9905 3 жыл бұрын
understood
@MJBZG
@MJBZG 6 ай бұрын
v good
@hitheshpk6030
@hitheshpk6030 2 жыл бұрын
UNDERSTOOD
@satvrii
@satvrii Жыл бұрын
@rishabhgautam7863
@rishabhgautam7863 3 жыл бұрын
Lol... maine do stacks se kiya tha ye phele hahaha
@abhishekpadhy903
@abhishekpadhy903 3 жыл бұрын
op explaination
@Shivi32590
@Shivi32590 7 ай бұрын
thank you
@kasyapdharanikota8570
@kasyapdharanikota8570 2 жыл бұрын
understood
@roughuse.jaiganesh
@roughuse.jaiganesh 2 жыл бұрын
understood.
@gaddamsaijyothi2659
@gaddamsaijyothi2659 Жыл бұрын
understood
@tanishkumar6682
@tanishkumar6682 Жыл бұрын
understood
Sliding Window Maximum | Leetcode
20:16
take U forward
Рет қаралды 267 М.
Big-O Notation - For Coding Interviews
20:38
NeetCode
Рет қаралды 551 М.
Mom Hack for Cooking Solo with a Little One! 🍳👶
00:15
5-Minute Crafts HOUSE
Рет қаралды 23 МЛН
Stack Implementation using a Single Queue
11:17
take U forward
Рет қаралды 194 М.
one year of studying (it was a mistake)
12:51
Jeffrey Codes
Рет қаралды 196 М.
The Last Algorithms Course You'll Need by ThePrimeagen | Preview
16:44
Frontend Masters
Рет қаралды 328 М.
Largest Rectangle in Histogram | Part - 1 | Leetcode 84
30:00
take U forward
Рет қаралды 226 М.
N meetings In One Room | Greedy Algorithm
18:00
take U forward
Рет қаралды 199 М.
Median of two Sorted Arrays of Different Sizes | Binary Search
31:54
take U forward
Рет қаралды 240 М.
DARKER SIDE of SOFTWARE ENGINEERING !!
12:09
Striver
Рет қаралды 147 М.
L4. Implement Min Stack | Stack and Queue Playlist
20:55
take U forward
Рет қаралды 61 М.
Next Greater Element | Two Variants | Leetcode
22:00
take U forward
Рет қаралды 260 М.
Mom Hack for Cooking Solo with a Little One! 🍳👶
00:15
5-Minute Crafts HOUSE
Рет қаралды 23 МЛН