Stack solution (with step by step explanation)
Intuition\nwe us stack to solve this problem\n# Approach\nwe create stack to store nodes with signature of { val, min }, in each node we will store minimum valu
# Intuition\nwe us stack to solve this problem\n# Approach\nwe create stack to store nodes with signature of { val, min }, in each node we will store minimum value for current value.\n# Complexity\n- Time complexity:\nO(1) -> each operation is constant time\n- Space complexity:\nO(n) -> we use stack to store nodes and this will give us n\n# Code\n```\nclass MinStack {\n    // declare private stack\n    private stack: { val: number, min: number}[];\n    // declare private size\n    private size: number;\n    constructor() {\n        // initialize stack with empty array\n        this.stack = [];\n        // initialize size to -1, this will give us ability to take last element like this.size instead of this.size - 1\n        this.size = -1;\n    }\n\n    push(val: number): void {\n        // check if stack is empty:\n        if(this.size < 0) {\n            // if true push first node to stack\n            this.stack.push({ val, min: val })\n        } else {\n            // if false calculate min value of current node and push it into stack\n            this.stack.push({ val, min: Math.min(this.stack[this.size].min, val)})\n        }\n        // increment size\n        this.size++;\n    }\n\n    pop(): void {\n        // pop node from stack\n        this.stack.pop();\n        // decrement size\n        this.size--;\n    }\n\n    top(): number {\n        // return val of top eleemnt from stack\n        return this.stack[this.size].val;\n    }\n\n    getMin(): number {\n        // return current minimum value of top stack node\n        return this.stack[this.size].min\n    }\n}\n\n/**\n * Your MinStack object will be instantiated and called as such:\n * var obj = new MinStack()\n * obj.push(val)\n * obj.pop()\n * var param_3 = obj.top()\n * var param_4 = obj.getMin()\n */\n```
['Array', 'Stack', 'TypeScript']