Skip to content

Instantly share code, notes, and snippets.

@4skinSkywalker
Created April 27, 2019 15:21
Show Gist options
  • Save 4skinSkywalker/c2a0aaad6e3126a959be7b87779ab4d9 to your computer and use it in GitHub Desktop.
Save 4skinSkywalker/c2a0aaad6e3126a959be7b87779ab4d9 to your computer and use it in GitHub Desktop.
class Stack {
constructor() {
this.array = []
}
push(value) {
this.array.push(value)
}
pop() {
return this.array.pop()
}
max() {
if (!this.array.length) {
return
}
let first = this.array.pop()
let temp = [first]
let max = first
while (this.array.length) {
let current = this.array.pop()
temp.push(current)
max = Math.max(max, current)
}
while (temp.length) {
this.array.push(temp.pop())
}
return max
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment