# 155. Minimum stack force buckle (LeetCode)

Released: 17:29:18, September 22, 2021

## Problem description and examples

Design a stack that supports push, pop and top operations and can retrieve the smallest element in a constant time.

push(x) -- pushes element x onto the stack.

pop() -- delete the element at the top of the stack.

top() -- get the stack top element.

getMin() -- retrieves the smallest element in the stack.

Input:

["MinStack","push","push","push","getMin","pop","top","getMin"]

[[],[-2],[0],[-3],[],[],[],[]]

Output:

[null,null,null,null,-3,null,0,-2]

Explanation:

MinStack minStack = new MinStack();

minStack.push(-2);

minStack.push(0);

minStack.push(-3);

minStack.getMin(); --> Return - 3

minStack.pop();

minStack.top(); --> Return 0

minStack.getMin(); --> Return - 2

Source: LeetCode

Link: https://leetcode-cn.com/problems/min-stack

The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

Tips:

pop, top, and getMin operations are always called on non empty stacks.

## My solution

Not long ago, I wrote a blog about using Map type data in JavaScript to realize stack structure (but it has nothing to do with this topic):

reference resources: [JavaScript Map] javascript uses Map type objects to implement stack structure_ Lai nianan's blog - CSDN blog

However, the key point of this problem is that if the minimum value in the stack is obtained in constant time, it means that the data in the stack cannot be sorted temporarily to obtain the minimum value when calling getMin() function. So I thought of using a special array to save the subscript of the "previous" minimum value in the current MinStack instance. The tail of this array stores the subscript of the minimum value of "current" in the current MinStack instance.

Please refer to the notes below for detailed explanation:

/** * initialize your data structure here. */ var MinStack = function() { // The stack attribute acts as the true stack structure in the MinStack instance this.stack = []; // The min attribute is also a stack structure, but it stores the subscript of the "previous" minimum value in the MinStack instance, // Therefore, the closer the value in min is to the top of the stack, the smaller the value. The top of the stack element stores the subscript of the minimum value of the "current" of the MinStack instance this.min = []; }; /** * @param {number} val * @return {void} */ MinStack.prototype.push = function(val) { // Pressing elements into the MinStack instance is actually stored in the stack attribute this.stack.push(val); // If the min stack of the current instance is empty, or the currently pressed element is smaller than the minimum value in the current instance if(!this.min.length || val < this.stack[this.min[this.min.length-1]]) { // Then it is necessary to update the direction of the minimum value in the current MinStack instance and announce the birth of a new minimum value this.min.push(this.stack.length-1); } }; /** * @return {void} */ MinStack.prototype.pop = function() { // If the pop-up stack top element happens to be the current minimum value, if(this.min[this.min.length-1] === this.stack.length-1) { // Then pop up the subscript saved at the top of the min stack this.min.pop(); } // stack does what it should do as usual this.stack.pop(); }; /** * @return {number} */ MinStack.prototype.top = function() { // The top element of the stack is the end element in the stack attribute return this.stack[this.stack.length - 1]; }; /** * @return {number} */ MinStack.prototype.getMin = function() { // The subscript of the minimum value in the current MinStack instance is the subscript saved at the top of the stack in the min of the instance return this.stack[this.min[this.min.length-1]]; }; Submit records Execution result: passed 31 / 31 Pass test cases Execution time: 112 ms, At all JavaScript Defeated 71 in submission.14%User Memory consumption: 44.8 MB, At all JavaScript Defeated 68 in submission.48%User Time: 2021/09/22 17:33

Because the problem requires to find the minimum value in the stack in constant time, an additional array is specially used to save the historical minimum value, which is a typical practice of exchanging time for space.

## Official explanation

Update: July 29, 2021 18:43:21

Because I consider the ownership of copyright, I won't paste the specific code in the [official solution], which can be viewed in the link below.

Update: September 22, 2021 17:34:17

reference resources: Minimum stack - minimum stack - LeetCode

[end of update]

## Relevant reference

Update: September 22, 2021 17:36:06

reference resources: [JavaScript Map] javascript uses Map type objects to implement stack structure_ Lai nianan's blog - CSDN blog