Easy way for it

why get our min and max value at the time of pushing, ?

i mean why dont we get our min and max value at the time of pushing that way it would be easier

You can try and code your idea as well. Check for some test cases, if it gives you the expected output.