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

  Рет қаралды 91,491

take U forward

take U forward

Күн бұрын

Пікірлер: 93
@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 ..
@shwetanksingh5208
@shwetanksingh5208 2 жыл бұрын
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 9 ай бұрын
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 !!
@mouthearnose
@mouthearnose 8 ай бұрын
Yeah these kind of questions are hard to think of during the interview if you haven't done these before.
@deepanshudahiya8966
@deepanshudahiya8966 4 ай бұрын
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
@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.
@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
@tulika2863
@tulika2863 3 жыл бұрын
Thank you , thank you so much for putting your efforts and this kind of high quality content . 🙏🏽🥺
@ytg6663
@ytg6663 2 жыл бұрын
🥺🥺
@producer_6_1_musics13
@producer_6_1_musics13 4 ай бұрын
encoded_value=2 * new_minimum - old_minimum old_minimum=2 * current_minimum - encoded_value...this generalization helped me a lot,
@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 4 ай бұрын
and?
@shantanugupta-oz1dx
@shantanugupta-oz1dx 4 ай бұрын
Thank you so much for explaining all the problems so well. Very helpful thank you so much again
@atharvacodes3705
@atharvacodes3705 3 жыл бұрын
Finally understood after watching 2 times and dry running. Keep up the good work! :)
@vaalarivan_p
@vaalarivan_p Жыл бұрын
but doesnt using a stack consume as much memory as using stack since the former is 64 bits?
@bhavyramani4456
@bhavyramani4456 5 ай бұрын
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.
@rutuja7293
@rutuja7293 3 жыл бұрын
Amazing and fruitful as always.Thank you for your efforts.
@joshipratik232
@joshipratik232 5 ай бұрын
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
@rohan8758
@rohan8758 3 ай бұрын
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.
@artofwrick
@artofwrick 9 ай бұрын
For leetcode, the answer wouldn't come at edge if you multiply with 2. Instead use add like min=min-st.peek()+min;
@manojnavinjamuri5867
@manojnavinjamuri5867 Жыл бұрын
Thank you soo much. This helped me to think the solution in different perspective.
@sayanmukherjee8563
@sayanmukherjee8563 3 жыл бұрын
Thank you so much for the great, simple and clear explanation.
@stith_pragya
@stith_pragya 6 ай бұрын
Understood.............Thank You So Much for this wonderful video..........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@aditi1729
@aditi1729 Жыл бұрын
why are we storing modified value in the stack?
@manaswitadatta
@manaswitadatta 3 жыл бұрын
Finally understood the proof clearly. Ty :)
@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 9 ай бұрын
Arithmetic Progression
@albedo9617
@albedo9617 4 ай бұрын
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?
@freshcontent3729
@freshcontent3729 3 жыл бұрын
bhaiya placements are starting, please upload many videos, go on full speed uploading ! :)
@AngadSingh97
@AngadSingh97 2 жыл бұрын
I am now having a great time solving problems everyday, wow
@aashishmittal79
@aashishmittal79 10 ай бұрын
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 9 ай бұрын
Yess. Infact using pair class is safe
@ashokdurunde1814
@ashokdurunde1814 2 жыл бұрын
Thank you , thank you so much for putting your efforts and this kind of high quality content . 🙏🏽
@siddharth794
@siddharth794 2 жыл бұрын
This videos should be in stack playlist
@Musicuvakavi1823
@Musicuvakavi1823 6 ай бұрын
Great teacher in the world
@geekyNib
@geekyNib 9 ай бұрын
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 9 ай бұрын
min=min-st.peek()+min
@scorcismweb5723
@scorcismweb5723 Жыл бұрын
2nd one is easy but im sure i wont be able to recall this
@techbus6016
@techbus6016 8 ай бұрын
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
@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
@risingengineers
@risingengineers 3 жыл бұрын
i wish i can meet you one day bhaia.
@vasujain1970
@vasujain1970 3 жыл бұрын
Great video as usual brother, enjoy your content a lot! Keep up the good work!
@sukhii0220
@sukhii0220 4 ай бұрын
Is no one concerned about it ? why we use formula in push opr 2*val-mini ? why we come with this ? please explain
@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.
@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 .
@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 9 ай бұрын
Cuz... You'll lose track of which was the last min and which was even last everytime you pop...
@Rahul_246a
@Rahul_246a 2 жыл бұрын
Wonderfully explained
@Karan-vq5vg
@Karan-vq5vg 3 жыл бұрын
please update the link in the website
@AmitSingh-ut4wt
@AmitSingh-ut4wt 2 жыл бұрын
Really amazing approach
@SaiTeja-ob6zg
@SaiTeja-ob6zg 2 жыл бұрын
wonderful explaination brother!
@adishijha8365
@adishijha8365 3 жыл бұрын
Amazing explanation!
@techwithindrani9376
@techwithindrani9376 2 жыл бұрын
Great content, thanks much for your effort. 😊
@_-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 😊
@sanskaarpatni9137
@sanskaarpatni9137 3 жыл бұрын
Amazing as always!
@AshishKumar-pq6pr
@AshishKumar-pq6pr 3 жыл бұрын
Awesome lecture
@codeman3828
@codeman3828 9 ай бұрын
Crazy optimal solution
@BECSAQUIB
@BECSAQUIB Жыл бұрын
You did not tell the intution behind the logic. You just verified it.
@vaalarivan_p
@vaalarivan_p Жыл бұрын
5:15 - 7:55
@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
@rishabh6210
@rishabh6210 5 ай бұрын
how is it better if we end up taking long long T_T
@ridj41
@ridj41 9 ай бұрын
2nd approach wasn't intuitutive at all.
@SuperWhatusername
@SuperWhatusername 2 жыл бұрын
understood Thanks
@sanskarrawat1047
@sanskarrawat1047 3 жыл бұрын
indeed indeed indeed...
@saouli1632
@saouli1632 9 ай бұрын
thank you bhiya❤🙏
@Learnprogramming-q7f
@Learnprogramming-q7f 8 ай бұрын
Thank you Bhaiya
@MJBZG
@MJBZG 4 ай бұрын
v good
@hitheshpk6030
@hitheshpk6030 2 жыл бұрын
UNDERSTOOD
@cypher7536
@cypher7536 2 жыл бұрын
Understood! 🔥
@rishabhgautam7863
@rishabhgautam7863 3 жыл бұрын
Lol... maine do stacks se kiya tha ye phele hahaha
@yushsingla9905
@yushsingla9905 3 жыл бұрын
understood
@satvrii
@satvrii Жыл бұрын
@abhishekpadhy903
@abhishekpadhy903 3 жыл бұрын
op explaination
@Shivi32590
@Shivi32590 5 ай бұрын
thank you
@kasyapdharanikota8570
@kasyapdharanikota8570 2 жыл бұрын
understood
@roughuse.jaiganesh
@roughuse.jaiganesh 2 жыл бұрын
understood.
@gaddamsaijyothi2659
@gaddamsaijyothi2659 Жыл бұрын
understood
@tanishkumar6682
@tanishkumar6682 Жыл бұрын
understood
L4. Implement Min Stack | Stack and Queue Playlist
20:55
take U forward
Рет қаралды 45 М.
Median of two Sorted Arrays of Different Sizes | Binary Search
31:54
take U forward
Рет қаралды 240 М.
風船をキャッチしろ!🎈 Balloon catch Challenges
00:57
はじめしゃちょー(hajime)
Рет қаралды 92 МЛН
Players push long pins through a cardboard box attempting to pop the balloon!
00:31
Симбу закрыли дома?! 🔒 #симба #симбочка #арти
00:41
Симбочка Пимпочка
Рет қаралды 4,4 МЛН
Turn Off the Vacum And Sit Back and Laugh 🤣
00:34
SKITSFUL
Рет қаралды 2,8 МЛН
Imlement LFU Cache | Leetcode(Hard)
19:27
take U forward
Рет қаралды 127 М.
Trapping Rainwater | Brute | Better | Optimal | with INTUITION
23:23
take U forward
Рет қаралды 279 М.
Next Greater Element | Two Variants | Leetcode
22:00
take U forward
Рет қаралды 257 М.
How I Mastered Data Structures and Algorithms in 8 Weeks
15:46
Aman Manazir
Рет қаралды 94 М.
Big-O Notation - For Coding Interviews
20:38
NeetCode
Рет қаралды 515 М.
Implement LRU Cache | Leetcode
17:05
take U forward
Рет қаралды 280 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 438 М.
DP 37. Buy and Sell Stocks III | Recursion to Space Optimisation
31:50
take U forward
Рет қаралды 177 М.
Merge Sorted Arrays Without Extra Space | 2 Optimal Solution
32:47
take U forward
Рет қаралды 215 М.
風船をキャッチしろ!🎈 Balloon catch Challenges
00:57
はじめしゃちょー(hajime)
Рет қаралды 92 МЛН