-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path155.min-stack.js
178 lines (162 loc) · 3.69 KB
/
155.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* @lc app=leetcode id=155 lang=javascript
*
* [155] Min Stack
*
* https://leetcode.com/problems/min-stack/description/
*
* algorithms
* Easy (38.37%)
* Likes: 2141
* Dislikes: 231
* Total Accepted: 353.5K
* Total Submissions: 907.2K
* Testcase Example: '["MinStack","push","push","push","getMin","pop","top","getMin"]\n[[],[-2],[0],[-3],[],[],[],[]]'
*
* 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.
*
*
* one storage for normal stack
* one storage for min stack that minimal always on top
*
* stack: -2 0 -3
* minStack: -2 -3
* stupidMinStackWhichStoreEachStepMin: -2 -2 -3
*/
// @lc code=start
/**
* initialize your data structure here.
*/
var MinStack = function () {
this.normalStack = [];
this.minStack = [];
};
/**
* @param {number} x
* @return {void}
*/
MinStack.prototype.push = function (x) {
this.normalStack.push(x);
if (
this.minStack.length === 0 ||
x <= this.minStack[this.minStack.length - 1]
) {
console.log("pushing to minstack", x, this.minStack);
this.minStack.push(x);
}
};
/**
* @return {void}
*/
MinStack.prototype.pop = function () {
let top = this.top();
this.normalStack.pop();
if (top === this.minStack[this.minStack.length - 1]) {
console.log("poping from min stack");
this.minStack.pop();
}
return top;
};
/**
* @return {number}
*/
MinStack.prototype.top = function () {
return this.normalStack[this.normalStack.length - 1];
};
/**
* @return {number}
*/
MinStack.prototype.getMin = function () {
//O(N) == =1
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()
*/
// @lc code=end
// /*
// Simple memory space solution
// // @lc code=start
// /**
// * initialize your data structure here.
// */
// var MinStack = function() {
// this.normalStack = [];
// this.minStack = [];
// };
// /**
// * @param {number} x
// * @return {void}
// */
// MinStack.prototype.push = function(x) {
// this.normalStack.push(x);
// // if (
// // this.minStack.length === 0 ||
// // x <= this.minStack[this.minStack.length - 1]
// // ) {
// // console.log("pushing to minstack", x, this.minStack);
// let elementToPush;
// if (this.minStack.length === 0) {
// elementToPush = x;
// } else {
// elementToPush = Math.min(x, this.minStack[this.minStack.length - 1]);
// }
// this.minStack.push(elementToPush);
// // }
// };
// /**
// * @return {void}
// */
// MinStack.prototype.pop = function() {
// let element = this.normalStack.pop();
// this.minStack.pop();
// return element;
// };
// /**
// * @return {number}
// */
// MinStack.prototype.top = function() {
// return this.normalStack[this.normalStack.length - 1];
// };
// /**
// * @return {number}
// */
// MinStack.prototype.getMin = function() {
// return this.minStack[this.normalStack.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()
// */
// // @lc code=end