Please like the video, helps a lot :) Also please spare a second to drop in a comment if you understood or not ..
@shwetanknaveen3 жыл бұрын
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 Жыл бұрын
In line no. 34, can we not reduce equation to "mini = elem;" (algebraically) ?
@artofwrick11 ай бұрын
The question had become medium in leetcode. Ahahahaha😂
@muhammedimdaad2 жыл бұрын
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--absh10 ай бұрын
Yeah these kind of questions are hard to think of during the interview if you haven't done these before.
@atharvacodes37053 жыл бұрын
Finally understood after watching 2 times and dry running. Keep up the good work! :)
@shuvbhowmickbestin Жыл бұрын
the level shouldn't be easy when asked to implement the solution at O(1) extra space. It should rather be medium/hard.
@tulika28633 жыл бұрын
Thank you , thank you so much for putting your efforts and this kind of high quality content . 🙏🏽🥺
@ytg66633 жыл бұрын
🥺🥺
@joshipratik2327 ай бұрын
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
@rupamrai94553 жыл бұрын
657 views and only 74 likes please each and everyone like and share this Precious content. Thanks, striver for your time and effort
@shivalikagupta34332 жыл бұрын
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_mohanreddy6 ай бұрын
and?
@rutuja72933 жыл бұрын
Amazing and fruitful as always.Thank you for your efforts.
@deepanshudahiya89666 ай бұрын
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 Жыл бұрын
but doesnt using a stack consume as much memory as using stack since the former is 64 bits?
@bhavyramani44567 ай бұрын
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.
@artofwrick11 ай бұрын
For leetcode, the answer wouldn't come at edge if you multiply with 2. Instead use add like min=min-st.peek()+min;
@rohan87585 ай бұрын
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-oz1dx6 ай бұрын
Thank you so much for explaining all the problems so well. Very helpful thank you so much again
@producer_6_1_musics135 ай бұрын
encoded_value=2 * new_minimum - old_minimum old_minimum=2 * current_minimum - encoded_value...this generalization helped me a lot,
@sayanmukherjee85633 жыл бұрын
Thank you so much for the great, simple and clear explanation.
@manojnavinjamuri5867 Жыл бұрын
Thank you soo much. This helped me to think the solution in different perspective.
@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?
@artofwrick11 ай бұрын
Yess. Infact using pair class is safe
@stith_pragya8 ай бұрын
Understood.............Thank You So Much for this wonderful video..........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@manaswitadatta3 жыл бұрын
Finally understood the proof clearly. Ty :)
@aditi1729 Жыл бұрын
why are we storing modified value in the stack?
@vasujain19703 жыл бұрын
Great video as usual brother, enjoy your content a lot! Keep up the good work!
@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 Жыл бұрын
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.
@artofwrick11 ай бұрын
Arithmetic Progression
@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..
@geekyNib11 ай бұрын
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)...
@artofwrick11 ай бұрын
min=min-st.peek()+min
@albedo96176 ай бұрын
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?
@ashokdurunde18142 жыл бұрын
Thank you , thank you so much for putting your efforts and this kind of high quality content . 🙏🏽
@MindsetMotivation752 жыл бұрын
In O(2N) 2 is just a constant . So , why we are considering this ?
@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 Жыл бұрын
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.
@Musicuvakavi18238 ай бұрын
Great teacher in the world
@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
@AngadSingh972 жыл бұрын
I am now having a great time solving problems everyday, wow
@Neil.Menezes16 күн бұрын
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 😄
@ridj4111 ай бұрын
2nd approach wasn't intuitutive at all.
@siddharth7942 жыл бұрын
This videos should be in stack playlist
@techbus601610 ай бұрын
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.
@aakritisharma52202 жыл бұрын
best teacher over youtube
@freshcontent37293 жыл бұрын
bhaiya placements are starting, please upload many videos, go on full speed uploading ! :)
@sukhii02206 ай бұрын
Is no one concerned about it ? why we use formula in push opr 2*val-mini ? why we come with this ? please explain
@aashayagarwal8903 Жыл бұрын
why are we storing the modified value?? just store the original and take min?
@gyaniyoda4608 Жыл бұрын
Time complexity will be O(n)
@artofwrick11 ай бұрын
Cuz... You'll lose track of which was the last min and which was even last everytime you pop...
@risingengineers3 жыл бұрын
i wish i can meet you one day bhaia.
@SaiTeja-ob6zg3 жыл бұрын
wonderful explaination brother!
@techwithindrani93762 жыл бұрын
Great content, thanks much for your effort. 😊
@scorcismweb5723 Жыл бұрын
2nd one is easy but im sure i wont be able to recall this
@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_Mahuri3 жыл бұрын
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-vq5vg3 жыл бұрын
please update the link in the website
@adishijha83653 жыл бұрын
Amazing explanation!
@AmitSingh-ut4wt2 жыл бұрын
Really amazing approach
@sanskaarpatni91373 жыл бұрын
Amazing as always!
@Rahul_246a2 жыл бұрын
Wonderfully explained
@_-69123 жыл бұрын
I understood the solution but I guess link is not reflecting in SDE sheet!
@takeUforward3 жыл бұрын
Forgot to add, will add it soon. Thanks.
@_-69123 жыл бұрын
@@takeUforward Thanks Raj 😊
@AshishKumar-pq6pr3 жыл бұрын
Awesome lecture
@saouli163211 ай бұрын
thank you bhiya❤🙏
@vaalarivan_p Жыл бұрын
5:15 - 7:55
@cypher75362 жыл бұрын
Understood! 🔥
@BECSAQUIB Жыл бұрын
You did not tell the intution behind the logic. You just verified it.
@SuperWhatusername2 жыл бұрын
understood Thanks
@Learnprogramming-q7f10 ай бұрын
Thank you Bhaiya
@rishabh62107 ай бұрын
how is it better if we end up taking long long T_T
@codeman382810 ай бұрын
Crazy optimal solution
@nikhil_squats3 жыл бұрын
Please make tree series
@stalker11643 жыл бұрын
He is saying this 102th time that Tree series comes after 100k subscribers
@sanskarrawat10473 жыл бұрын
indeed indeed indeed...
@yushsingla99053 жыл бұрын
understood
@MJBZG6 ай бұрын
v good
@hitheshpk60302 жыл бұрын
UNDERSTOOD
@satvrii Жыл бұрын
❤
@rishabhgautam78633 жыл бұрын
Lol... maine do stacks se kiya tha ye phele hahaha