File tree 1 file changed +8
-8
lines changed
scripts/algorithms/M/Min Stack 1 file changed +8
-8
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 29 ms (Top 73.41%) | Memory: 16.5 MB (Top 10.27%)
1
2
class MinStack {
2
3
public:
3
4
stack<int > stk;
4
5
stack<int > minstk;
5
-
6
+
6
7
MinStack () {
7
8
// leave empty because we don't need to initialise an object while it being created
8
9
}
9
-
10
+
10
11
void push (int val) {
11
- // ---------------Push in original stack------------------------------
12
+ // ---------------Push in original stack------------------------------
12
13
stk.push (val);
13
- // -------Put always minimum element to MIN stack--------------
14
+ // -------Put always minimum element to MIN stack--------------
14
15
if (minstk.size ()==0 )
15
16
minstk.push (val);
16
17
else {
@@ -20,18 +21,17 @@ class MinStack {
20
21
minstk.push (val);
21
22
}
22
23
}
23
-
24
+
24
25
void pop () {
25
26
stk.pop ();
26
27
minstk.pop ();
27
28
}
28
-
29
+
29
30
int top () {
30
31
return stk.top ();
31
32
}
32
-
33
+
33
34
int getMin () {
34
35
return minstk.top ();
35
36
}
36
37
};
37
-
You can’t perform that action at this time.
0 commit comments