-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0155. Min Stack.js
135 lines (114 loc) · 2.87 KB
/
0155. Min Stack.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
// push(x) -- Push element x onto stack.
// pop() -- Removes the element on top of the stack.
// top() -- Get the top element.
// getMin() -- Retrieve the minimum element in the stack.
// Example:
// MinStack minStack = new MinStack();
// minStack.push(-2);
// minStack.push(0);
// minStack.push(-3);
// minStack.getMin(); --> Returns -3.
// minStack.pop();
// minStack.top(); --> Returns 0.
// minStack.getMin(); --> Returns -2.
// 1) two stack
// https://leetcode.com/problems/min-stack/discuss/49185/MinStack-JavaScript-solution
/**
* initialize your data structure here.
*/
let MinStack = function() {
this.minStack = []
this.container = []
}
/**
* @param {number} x
* @return {void}
*/
MinStack.prototype.push = function(x) {
this.container.push(x)
if (!this.minStack.length || x <= this.minStack[this.minStack.length - 1]) {
this.minStack.push(x)
}
}
/**
* @return {void}
*/
MinStack.prototype.pop = function() {
let x = this.container.pop()
if (x === this.minStack[this.minStack.length - 1]) {
this.minStack.pop()
}
}
/**
* @return {number}
*/
MinStack.prototype.top = function() {
return this.container[this.container.length - 1]
}
/**
* @return {number}
*/
MinStack.prototype.getMin = function() {
return this.minStack[this.minStack.length - 1]
}
/**
* Your MinStack object will be instantiated and called as such:
* var obj = new MinStack()
* obj.push(x)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.getMin()
*/
// Runtime: 124 ms, faster than 36.09% of JavaScript online submissions for Min Stack.
// Memory Usage: 44.4 MB, less than 37.50% of JavaScript online submissions for Min Stack.
// 2) one stack
// https://leetcode.com/problems/min-stack/discuss/49014/Java-accepted-solution-using-one-stack
/**
* initialize your data structure here.
*/
let MinStack = function() {
this.minStack = []
this.min = Infinity
}
/**
* @param {number} x
* @return {void}
*/
MinStack.prototype.push = function(x) {
if (x <= this.min) {
this.minStack.push(this.min)
this.min = x
}
this.minStack.push(x)
}
/**
* @return {void}
*/
MinStack.prototype.pop = function() {
if (this.minStack.pop() === this.min) {
this.min = this.minStack.pop()
}
}
/**
* @return {number}
*/
MinStack.prototype.top = function() {
return this.minStack[this.minStack.length - 1]
}
/**
* @return {number}
*/
MinStack.prototype.getMin = function() {
return this.min
}
/**
* Your MinStack object will be instantiated and called as such:
* var obj = new MinStack()
* obj.push(x)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.getMin()
*/
// Runtime: 100 ms, faster than 88.42% of JavaScript online submissions for Min Stack.
// Memory Usage: 44.7 MB, less than 25.00% of JavaScript online submissions for Min Stack.