-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargestNumberStack.js
111 lines (76 loc) · 2.09 KB
/
largestNumberStack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// You want to be able to access the largest element in a stack. ↴
// Use your Stack class to implement a new class MaxStack with a method getMax() that returns the largest element in the stack. getMax() should not remove the item.
// Can also implement a Max Heap to find the answer. Will be O(n) space.
// Implement the push, pop, and getMax methods
class MaxStack {
constructor() {
this.stack = new Stack();
this.maxStack = new Stack();
}
push(item) {
this.stack.push(item);
if (this.maxStack.peek() === null || item >= this.maxStack.peek()) {
this.maxStack.push(item);
}
}
pop() {
let item = this.stack.pop();
if (item === this.maxStack.peek()) {
this.maxStack.pop();
}
return item;
}
getMax() {
return this.maxStack.peek();
}
}
class Stack {
constructor() {
// Initialize an empty array
this.items = [];
}
// Push a new item to the last index
push(item) {
this.items.push(item);
}
// Remove the last item
pop() {
// If the stack is empty, return null
// (It would also be reasonable to throw an exception)
if (!this.items.length) {
return null;
}
return this.items.pop();
}
// See what the last item is
peek() {
if (!this.items.length) {
return null;
}
return this.items[this.items.length - 1];
}
}
// Tests
const s = new MaxStack();
s.push(5);
assertEquals(5, s.getMax(), 'check max after 1st push');
s.push(4);
s.push(7);
s.push(7);
s.push(8);
assertEquals(8, s.getMax(), 'check before 1st pop');
assertEquals(8, s.pop(), 'check pop #1');
assertEquals(7, s.getMax(), 'check max after 1st pop');
assertEquals(7, s.pop(), 'check pop #2');
assertEquals(7, s.getMax(), 'check max after 2nd pop');
assertEquals(7, s.pop(), 'check pop #3');
assertEquals(5, s.getMax(), 'check max after 3rd pop');
assertEquals(4, s.pop(), 'check pop #4');
assertEquals(5, s.getMax(), 'check max after 4th pop');
function assertEquals(a, b, desc) {
if (a === b) {
console.log(`${desc} ... PASS`);
} else {
console.log(`${desc} ... FAIL: ${a} != ${b}`);
}
}