Created
April 27, 2019 15:21
-
-
Save 4skinSkywalker/c2a0aaad6e3126a959be7b87779ab4d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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