> For the complete documentation index, see [llms.txt](https://algorithm.prettylog.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/min-max-stack-construction.md).

# Min Max Stack Construction

![](/files/oUAo9T5QaNXUFLVDkYZ8)

* 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;
```
