✔️ Simple to understand 2 Ways, Stack & List, With Comments
Thumbs up if you find this helpful \uD83D\uDC4D\n\nThis solution is inspired by the hint Consider each node in the stack having a minimum value.\n\nThe main ide
**Thumbs up if you find this helpful** \uD83D\uDC4D\n\nThis solution is inspired by the hint ```Consider each node in the stack having a minimum value.```\n\nThe main idea of this solution is to have a way to keep track of the minimum value every time an item is pushed onto the stack. \n\nWhen a new value is pushed onto the stack, it is checked against the last minimum to see if the current value is a minimum. Every time a new item is added, we keep track of the minimum value for each push.\n\nIn both solutions:\n* When the ```Top``` function is called, the current top value is returned.\n* When the ```GetMin``` function is called, the minimum value at the time top value was pushed onto the stack is returned\n* When the ```Push``` function is called, the value and the current minimum value is pushed onto the stack\n\n**1. Using Stack**\n\nIn this solution, we use a stack<pair<int, int>> to keep track of the current item pushed onto the stack, and the minimum value at the time the item was pushed onto the stack.\n\n```\npublic class MinStack {\n    private Stack<KeyValuePair<int, int>> data;\n    \n    /** initialize your data structure here. */\n    public MinStack() {\n        // Initialize the stack\n        // The pair key is the stack value\n        // The pair value is the current stack minimum value\n        data = new Stack<KeyValuePair<int, int>>();\n    }\n    \n    public void Push(int val) {\n        // Determine minimum value.\n        // If the stack is not empty, compare against the last min value\n        var currentMinValue = val;    \n        if (data.Count > 0) {\n            currentMinValue = Math.Min(currentMinValue, GetMin());\n        }\n        \n        // Add new entry to the stack, saving the value and current minimum\n        data.Push(new KeyValuePair<int, int>(val, currentMinValue));\n    }\n    \n    public void Pop() {\n        data.Pop();\n    }\n    \n    public int Top() {\n        // Return the current top value\n        return data.Peek().Key;\n    }\n    \n    public int GetMin() {        \n        // Return the current minimum value\n        return data.Peek().Value;        \n    }\n}\n ```\n \n \n**2. Using List**\n\nIn this solution, we use a list<pair<int, int>> to keep track of the current item pushed onto the stack, and the minimum value at the time the item was pushed onto the stack.\n\n ```\npublic class MinStack {\n    private List<KeyValuePair<int, int>> data;\n    \n    /** initialize your data structure here. */\n    public MinStack() {\n        // Initialize the list\n        // The pair key is the stack value\n        // The pair value is the current stack minimum value\n        data = new List<KeyValuePair<int, int>>();\n    }\n    \n    public void Push(int val) {\n        // Determine minimum value.\n        // If the stack is not empty, compare against the last min value\n        var currentMinValue = val;    \n        if (data.Count > 0) {\n            currentMinValue = Math.Min(currentMinValue, GetMin());\n        }\n        \n        // Add new entry to the stack, saving the value and current minimum\n        data.Add(new KeyValuePair<int, int>(val, currentMinValue));\n    }\n    \n    public void Pop() {\n        data.RemoveAt(data.Count - 1);\n    }\n    \n    public int Top() {\n        // Return the current top value\n        return data[data.Count - 1].Key;\n    }\n    \n    public int GetMin() {        \n        // Return the current minimum value\n        return data[data.Count - 1].Value;        \n    }\n}\n```
[]