Theoretical doubt --> Can't understand why we are following this approach.. It is vague and confusing.. help

In the video’s begining goal was to find min element in stack in o(1) time and space … so why not just simply keep track of the element being inserted and have a currentMin variable which compares minimum of the prev min and element going to be inserted and why do we need to do 2*x - current_min operation

we are at the end having the final ans as 3 but it should be 1 as it is min in stack … I am bit confused , what is going on in the video and approach?

@anmol1911
hello Anmol ,
the approach u are saying is correct ,but it takes o(N) space because u will store one extra variable for each stack element. and if number of stack element is n then u will use n variables to store minimum at each level which is O(n).

this is the only reason we are opting this 2*x-cur_min trick…

pls refer this article -> https://www.geeksforgeeks.org/design-a-stack-that-supports-getmin-in-o1-time-and-o1-extra-space/

Ok, I get the problem and the approach… thanks for the article and quick reply :smile: