-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathminStack.js
More file actions
89 lines (76 loc) · 1.88 KB
/
minStack.js
File metadata and controls
89 lines (76 loc) · 1.88 KB
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
/*
https://leetcode.com/problems/min-stack/
Design a stack class that supports the push, pop, top, and getMin operations.
MinStack() initializes the stack object.
void push(int val) pushes the element val onto the stack.
void pop() removes the element on the top of the stack.
int top() gets the top element of the stack.
int getMin() retrieves the minimum element in the stack.
Each function should run in O(1) time.
Example 1:
Input: ["MinStack", "push", 1, "push", 2, "push", 0, "getMin", "pop", "top", "getMin"]
Output: [null,null,null,null,0,null,2,1]
Explanation:
MinStack minStack = new MinStack();
minStack.push(1);
minStack.push(2);
minStack.push(0);
minStack.getMin(); // return 0
minStack.pop();
minStack.top(); // return 2
minStack.getMin(); // return 1
Constraints:
-2^31 <= val <= 2^31 - 1.
pop, top and getMin will always be called on non-empty stacks.
*/
class MinStack {
constructor() {
this.min = Infinity;
this.stack = [];
}
/**
* @param {number} val
* @return {void}
*/
push(val) {
if (this.stack.length === 0) {
this.stack.push(0);
this.min = val;
} else {
this.stack.push(val - this.min);
if (val < this.min) {
this.min = val;
}
}
}
/**
* @return {void}
*/
pop() {
if (this.stack.length === 0) {
return;
}
const top = this.stack.pop();
if (top < 0) {
this.min = this.min - top;
}
}
/**
* @return {number}
*/
top() {
const top = this.stack[this.stack.length - 1];
if (top !== undefined && top > 0) {
return top + this.min;
} else {
return this.min;
}
}
/**
* @return {number}
*/
getMin() {
return this.min;
}
}
module.exports = { MinStack };