Leetcode MinStack Design Problem: JavaScript Low Level Solution
Tags: algorithms, javascript, design
Published: 2020-12-22 12:32:30

With this post, I would like to share a low level Javascript solution for one of the most popular design problems from leetcode  it is called MinStack.The problem expect a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Here instead of using Javascript's `length`, `Math.min()`, `push`, `pop` etc. , the expected functions are written at the low level as possible.
```javascript
/**
 * initialize data structure
 */
var MinStack = function () {

    this.stack = [];
    this.minVals = [];
    this.count = 0;
    this.min = Number.MAX_VALUE;
};

/** 
 * @param {number} x
 * @return {void}
 */
MinStack.prototype.push = function (x) {

    if (x < this.min || this.count === 0) {
        this.min = x;
    }

    this.minVals[this.count] = this.min;
    this.stack[this.count] = x;
    this.count++;

};

/**
 * @return {void}
 */
MinStack.prototype.pop = function () {

    delete this.stack[this.count - 1];
    delete this.minVals[this.count - 1];
    this.min = this.minVals[this.count - 2];
    this.count--;


};

/**
 * @return {number}
 */
MinStack.prototype.top = function () {

    return this.stack[this.count - 1]

};

/**
 * @return {number}
 */
MinStack.prototype.getMin = function () {

    if (this.count > 0)
        return this.minVals[this.count - 1];
        
    else if (this.count === 0)
        return this.MinVals[0]
};


```
 
#### MinStack: 
* stack: is a storage of the values, in other words, the stack
* minVals: is a storage for the minimum Values; it is needed because after pop() operation we might delete the minimum value of the stack so the minimum values are also should be tracked.
* count: instead of using Javascript's `length` we can use `this.count` in order to set the last index and get the value of stack's last value.
* min: this is a kind of initial and global minimum value, that starts with the maximum numeric value in Javascript.

#### push(x):
 Adds an element to the stack. Does not return any value.
Here, while adding new element(x) to the stack we also check that whether new element is less than the `this.min` value. Also include the case that, if the stack is empty,  means `this.count===0`, then of course the first element of `this.minVals` is also equal to the new element(x). Both length of the stacks are equal which is the value of `count`.
Then instead of using Javascript's build in push() function, we say the last element of the stack is the new element:
```javascript
...

    if (x < this.min || this.count === 0) {
        this.min = x;
    }

    this.minVals[this.count] = this.min;
    this.stack[this.count] = x;
    this.count++;
```
#### pop():
Removes the last item from the stack. We are not going to use pop() function of Javascript. We just going to delete the last item from the stack. We need to consider that, *maybe* the value which is *going to be removed* is the minimum value of the array. So actually thats why we've needed an extra `minVals` stack instead of only with a `this.min`. In order to catch the new state of the stack, we need to also delete the last item of the `minVals` stack. But!
We also should remember that when we add new value to the stack with our `push(x)` function that is given above. There, we compare the *x* value with the `this.min` value, thats why `this.min` value is no longer the last item of the `this.minVals` stack but the previous one. And lastly, since we removed the last item from stack we should decrease the count number, so that when next time  we do another operation, it should follow the last index of the stack with the count value.  
```javascript
...
    delete this.stack[this.count - 1];
    this.min = this.minVals[this.count - 2];
    delete this.minVals[this.count - 1];
    this.count--;
```

#### top():
Returns the last item of the stack.
Here the length of the stack is equal to `this.count` and the last item of the stack is at the index of `this.count-1`:
```javascript
 return this.stack[this.count - 1]
```
#### getMin():
Returns the minium value of the stack. Here instead of scanning the whole stack, since we already set the minimum value to the `minStack` all the time. We can comfartably return the last item of minStack. Remember the last item's index is `this.count-1`; but if we are already at `count=0` then we simply should return the first value of the minStack:
```javascript
...
if (this.count === 0)
        return this.MinVals[0]
    else
        return this.minVals[this.count - 1];

```