Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Min Stack Algorithm JS

function last(stack){
    return stack[stack.length - 1]
}
class MinStack {
    constructor(){
        this.stack = []
        this.minStack = []
    }
push(x){
        if(this.minStack.length === 0 || x <= last(this.minStack)){
            this.minStack.push(x)
        }
        this.stack.push(x)
    }
    
    pop(){
        if(last(this.minStack) === last(this.stack)){
            this.minStack.pop()
        }
        return this.stack.pop()
    }
    
    top(){
        return last(this.stack)
    }
    
    getMin(){
        return last(this.minStack)
    }
    
    
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript list include 
Javascript :: owl carousel next previous button 
Javascript :: how to check if a javascript object is empty 
Javascript :: javascript sort array of objects by property alphabetically 
Javascript :: react add link to button 
Javascript :: @angular/common@11.2.1 node_modules/@angular/common @angular/common@"11.2.1" from the root project 
Javascript :: get data from formdata 
Javascript :: typed.js 
Javascript :: mongoose schema cast decimals 
Javascript :: pass variable to regrex literal notation javascript 
Javascript :: how to get url params value in node js 
Javascript :: add class to element javascript 
Javascript :: jwt token expire time in node js 
Javascript :: javascript Inserting values in between an array 
Javascript :: javascript canvas grayscale 
Javascript :: what is the correct json content type 
Javascript :: firebase order by key descending 
Javascript :: jquery active menu 
Javascript :: short if 
Javascript :: jquery insert after element 
Javascript :: how to disable keyboard input in javascript 
Javascript :: node how to stop NodeJS server process 
Javascript :: On pressing enter change the focus to the next input field 
Javascript :: reactjs hello world 
Javascript :: filereader check file type 
Javascript :: axios react 
Javascript :: get all the child of the same class javascript 
Javascript :: Terminating timed out worker 
Javascript :: array without duplicates js 
Javascript :: show hidden element javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =