Simple c++ solution using a single stack(No struct needed too), 100% time and space efficient
I decided to post this solution as I found in the discussion tab that mainly people discussed about solutions using 2 stacks or using 2 lists or using structs a
I decided to post this solution as I found in the discussion tab that mainly people discussed about solutions using 2 stacks or using 2 lists or using structs along with list. This solution is **100% TIME and SPACE efficient**  and uses a simple trick to get us the minimum element in the stack using O(1) time. See the top( ), pop( ) and push( ) methods to see the little tweak used. \n\nInstead of pushing in the element which is smaller than the current minimum we push in **2 * x - min** (where x is the element we are currently pushing and min is the current minimum) and update the variable min. Similarly, when we pop, if the top element is smaller than the current min, we update min by, **min = 2 * min - s.top()** and then pop the element. We also have to keep in mind that in the top( ) function if the current top element is smaller than min then we need to **return min**.\n\n```\n\nclass MinStack {\npublic:\n    /** initialize your data structure here. */\n    stack< long double > s ;\n    long double min ;\n    MinStack() {\n        \n    }\n    \n    void push(long double x) {\n        if( s.empty() == true ){\n           s.push(x) ;\n           min = x ;\n        }else{\n           if( x < min ){\n               s.push( 2 * x - min ) ;\n               min = x ;\n               \n           }else{\n              s.push( x ) ;\n           }\n        }\n    }\n    \n    void pop() {\n        if( s.empty() == false ){\n            if( s.top() > min ){\n                s.pop() ;\n            }else{\n                min = 2 * min - s.top() ;\n                s.pop() ;\n            }\n        }\n    }\n    \n    int top() {\n        if( s.top() > min )\n            return s.top() ;\n        else\n            return min ;\n    }\n    \n    int getMin() {\n        return min ;\n    }\n};\n \n ```
[]