Last active
August 9, 2017 02:36
-
-
Save victorkurauchi/85eb068c0df2cdfe69ade1f96b60881f to your computer and use it in GitHub Desktop.
Javascript arr methods Big O
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 PilhaInteiros { | |
| constructor (arr) { | |
| // ex new PilhaInteiros([1, 3, 6, 8]) | |
| this.pilha = arr | |
| if (arr && arr.length) { | |
| let minIdx | |
| arr.forEach((n, idx) => { | |
| if (typeof minIdx == 'undefined') { | |
| minIdx = idx | |
| } | |
| if (n < arr[minIdx]) { | |
| minIdx = idx | |
| } | |
| }) | |
| this.minValueIdx = minIdx | |
| } else { | |
| this.minValueIdx = 0 | |
| } | |
| } | |
| show () { | |
| return this.pilha | |
| } | |
| push (n) { | |
| let idx = this.pilha.length | |
| this.pilha[idx] = n | |
| if (this.pilha[idx] < this.pilha[this.minValueIdx]) { | |
| this.minValueIdx = idx | |
| } | |
| return this.pilha | |
| } | |
| pop () { | |
| this.pilha.pop() | |
| return this.pilha | |
| } | |
| min () { | |
| return this.pilha[this.minValueIdx] | |
| } | |
| } | |
| const pilha = new PilhaInteiros([2, 3, 6, 8,1]) | |
| pilha.push(10) | |
| pilha.push(20) | |
| console.log(pilha.min()) | |
| console.log(pilha.show()) | |
| pilha.pop() | |
| console.log(pilha.show()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment