forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (47 loc) · 1.24 KB
/
index.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
/**
You have to implement the minStack class which will have a min() function.
Whenever min() is called, the minimum value of the stack is returned in O(1) time.
The element is not popped from the stack, its value is simply returned.
Keep in mind that the min function should work in O(1) and should not pop
the minimum element out of the stack. It simply returns its value.
*/
const Stack = require('../index');
class MinStack {
constructor() {
this.main = new Stack();
this.minStack = new Stack();
}
push(element) {
this.main.push(element);
if (!this.minStack.peek()) {
return this.minStack.push(element);
}
if (element > this.minStack.peek()) {
return this.minStack.push(this.minStack.peek());
}
return this.minStack.push(element);
}
pop() {
this.minStack.pop();
return this.main.pop();
}
getMin() {
return this.minStack.peek();
}
}
const ms = new MinStack();
ms.push(1);
ms.push(10);
ms.push(21);
ms.push(3);
ms.push(9);
ms.push(-11);
ms.push(32);
// eslint-disable-next-line no-console
console.log(ms.minStack.data);
// eslint-disable-next-line no-console
console.log(ms.getMin());
ms.pop();
ms.pop();
// eslint-disable-next-line no-console
console.log(ms.getMin());