Share my clean AC C++ solution, with explanation.
The key idea is use a another stack to store the minimum value of the corresponding stack. Put differently, min[i] equals the minimum element where data[i] is t
The key idea is use a another stack to store the `minimum value` of the corresponding stack. Put differently, `min[i]` equals the minimum element where `data[i]` is the top of this sub-stack.\n\nWe can use a full size of `min` where its size equals the data's, but it's not necessary.\n\n**Idea**\n\n*  We should `pop` the element in `min` IFF there's match of `data.top()`.\n\n* If we have multiple same minima, for example `[0, 1, 0]` in `data`, then the `min` should be `[0, 0]`.\nOtherwise, the the `pop` operation wouldn't work properly, since that you need 2 `0`s.\nAs a result, we should push the element if `x <= min.top()`.\n\n\n**Code**\n\n    class MinStack {\n        \n        stack<int> data;\n        stack<int> min;\n\n    public:\n\n        void push(int x) {\n            \n            // If empty\n            if (min.empty()) {\n                data.push(x);\n                min.push(x);\n            }\n            \n            // Not empty\n            else {\n                data.push(x);\n                if (x <= min.top())\n                    min.push(x);\n            }\n\n        }\n\n        void pop() {\n            \n            if (!min.empty()) {\n                if (data.top() == min.top())\n                    min.pop();\n                data.pop();\n            }\n        }\n\n        int top() {\n            return data.top();\n        }\n\n        int getMin() {\n            return min.top();\n        }\n    };
['C++']