-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_3.2.js
44 lines (43 loc) · 970 Bytes
/
problem_3.2.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
// Problem #3.2:
// How would you design a stack which, in addition to push and pop, has a function min which returns the minimum element?
// Push, pop and min should all operate in 0(1) time.
class Stack {
constructor() {
this.data = [];
this.mins = [];
this.size = 0;
}
push(value) {
this.mins.push(this.size > 0 ? Math.min(this.mins[this.size-1], value) : value);
this.data.push(value);
this.size++;
}
pop() {
this.mins.pop();
this.size--;
return this.data.pop();
}
top() {
return this.data[this.size - 1];
}
getMin() {
return this.mins[this.size - 1];
}
size() {
return this.size;
}
isEmpty() {
return this.size === 0;
}
}
let stack = new Stack();
stack.push(3);
stack.push(4);
stack.push(2);
stack.push(1);
stack.push(5);
console.log(stack.top()); // 5
console.log(stack.getMin()); // 1
console.log(stack.pop()); // 5
console.log(stack.pop()); // 1
console.log(stack.getMin()); // 2