# Min Max Stack Construction

![](https://3743232000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHW2IQuh2PFpWJDvBz2FF%2Fuploads%2Fgit-blob-659967f02fb663f8bfe8c5d66f5fea1fdeb872a4%2FScreenshot%202023-01-25%20at%201.45.04.png?alt=media)

* 1, 1

```jsx
// Feel free to add new properties and methods to the class.
class MinMaxStack {

  constructor() {
    this.stack = []; 
    this.minMax = [];
    this.min = Infinity;
    this.max = -Infinity;
  }
  
  peek() {
    return this.stack[this.stack.length - 1];
  }

  pop() {
    this.minMax.pop();
    return this.stack.pop();
  }

  push(number) {
    this.stack.push(number);
    const nextMinMax = [Math.min(this.getMin(), number), Math.max(this.getMax(), number)];
    this.minMax.push(nextMinMax);
  }

  getMin() {
    return this.getMinMax()[0];
  }

  getMax() {
    return this.getMinMax()[1];
  }

  getMinMax() {
    const minMax = this.minMax[this.minMax.length - 1];    
    if (minMax === undefined) return [Infinity, -Infinity];
    return minMax;
  }
}

// Do not edit the line below.
exports.MinMaxStack = MinMaxStack;
```
