Please like the video, helps a lot :) Also please spare a second to drop in a comment if you understood or not ..
@shwetanksingh52082 жыл бұрын
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) ?
@artofwrick9 ай бұрын
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 !!
@mouthearnose8 ай бұрын
Yeah these kind of questions are hard to think of during the interview if you haven't done these before.
@deepanshudahiya89664 ай бұрын
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 Жыл бұрын
the level shouldn't be easy when asked to implement the solution at O(1) extra space. It should rather be medium/hard.
@rupamrai94553 жыл бұрын
657 views and only 74 likes please each and everyone like and share this Precious content. Thanks, striver for your time and effort
@tulika28633 жыл бұрын
Thank you , thank you so much for putting your efforts and this kind of high quality content . 🙏🏽🥺
@ytg66632 жыл бұрын
🥺🥺
@producer_6_1_musics134 ай бұрын
encoded_value=2 * new_minimum - old_minimum old_minimum=2 * current_minimum - encoded_value...this generalization helped me a lot,
@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_mohanreddy4 ай бұрын
and?
@shantanugupta-oz1dx4 ай бұрын
Thank you so much for explaining all the problems so well. Very helpful thank you so much again
@atharvacodes37053 жыл бұрын
Finally understood after watching 2 times and dry running. Keep up the good work! :)
@vaalarivan_p Жыл бұрын
but doesnt using a stack consume as much memory as using stack since the former is 64 bits?
@bhavyramani44565 ай бұрын
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.
@rutuja72933 жыл бұрын
Amazing and fruitful as always.Thank you for your efforts.
@joshipratik2325 ай бұрын
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
@rohan87583 ай бұрын
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.
@artofwrick9 ай бұрын
For leetcode, the answer wouldn't come at edge if you multiply with 2. Instead use add like min=min-st.peek()+min;
@manojnavinjamuri5867 Жыл бұрын
Thank you soo much. This helped me to think the solution in different perspective.
@sayanmukherjee85633 жыл бұрын
Thank you so much for the great, simple and clear explanation.
@stith_pragya6 ай бұрын
Understood.............Thank You So Much for this wonderful video..........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@aditi1729 Жыл бұрын
why are we storing modified value in the stack?
@manaswitadatta3 жыл бұрын
Finally understood the proof clearly. Ty :)
@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.
@artofwrick9 ай бұрын
Arithmetic Progression
@albedo96174 ай бұрын
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?
@freshcontent37293 жыл бұрын
bhaiya placements are starting, please upload many videos, go on full speed uploading ! :)
@AngadSingh972 жыл бұрын
I am now having a great time solving problems everyday, wow
@aashishmittal7910 ай бұрын
Isn't the optimal solution O(2N) space as well because we are using long long which is double the size of int datatype?
@artofwrick9 ай бұрын
Yess. Infact using pair class is safe
@ashokdurunde18142 жыл бұрын
Thank you , thank you so much for putting your efforts and this kind of high quality content . 🙏🏽
@siddharth7942 жыл бұрын
This videos should be in stack playlist
@Musicuvakavi18236 ай бұрын
Great teacher in the world
@geekyNib9 ай бұрын
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)...
@artofwrick9 ай бұрын
min=min-st.peek()+min
@scorcismweb5723 Жыл бұрын
2nd one is easy but im sure i wont be able to recall this
@techbus60168 ай бұрын
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
@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
@risingengineers3 жыл бұрын
i wish i can meet you one day bhaia.
@vasujain19703 жыл бұрын
Great video as usual brother, enjoy your content a lot! Keep up the good work!
@sukhii02204 ай бұрын
Is no one concerned about it ? why we use formula in push opr 2*val-mini ? why we come with this ? please explain
@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.
@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 .
@aashayagarwal8903 Жыл бұрын
why are we storing the modified value?? just store the original and take min?
@gyaniyoda4608 Жыл бұрын
Time complexity will be O(n)
@artofwrick9 ай бұрын
Cuz... You'll lose track of which was the last min and which was even last everytime you pop...
@Rahul_246a2 жыл бұрын
Wonderfully explained
@Karan-vq5vg3 жыл бұрын
please update the link in the website
@AmitSingh-ut4wt2 жыл бұрын
Really amazing approach
@SaiTeja-ob6zg2 жыл бұрын
wonderful explaination brother!
@adishijha83653 жыл бұрын
Amazing explanation!
@techwithindrani93762 жыл бұрын
Great content, thanks much for your effort. 😊
@_-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 😊
@sanskaarpatni91373 жыл бұрын
Amazing as always!
@AshishKumar-pq6pr3 жыл бұрын
Awesome lecture
@codeman38289 ай бұрын
Crazy optimal solution
@BECSAQUIB Жыл бұрын
You did not tell the intution behind the logic. You just verified it.
@vaalarivan_p Жыл бұрын
5:15 - 7:55
@nikhil_squats3 жыл бұрын
Please make tree series
@stalker11643 жыл бұрын
He is saying this 102th time that Tree series comes after 100k subscribers
@rishabh62105 ай бұрын
how is it better if we end up taking long long T_T
@ridj419 ай бұрын
2nd approach wasn't intuitutive at all.
@SuperWhatusername2 жыл бұрын
understood Thanks
@sanskarrawat10473 жыл бұрын
indeed indeed indeed...
@saouli16329 ай бұрын
thank you bhiya❤🙏
@Learnprogramming-q7f8 ай бұрын
Thank you Bhaiya
@MJBZG4 ай бұрын
v good
@hitheshpk60302 жыл бұрын
UNDERSTOOD
@cypher75362 жыл бұрын
Understood! 🔥
@rishabhgautam78633 жыл бұрын
Lol... maine do stacks se kiya tha ye phele hahaha