You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
if(s.empty()) { //if stack is empty just push and set it as min
13
+
s.push(x);
14
+
min = x;
15
+
}
16
+
else{
17
+
if(x < min) { //if current value is less than current min, create flag in stack as 2 * x - min which will allow to traverse amongst next min while popping
18
+
s.push(2 * x - min);
19
+
min = x;
20
+
}
21
+
else
22
+
s.push(x); //just push current x
23
+
}
24
+
}
25
+
26
+
voidpop() {
27
+
if(!s.empty()){ //if stack is non empty
28
+
if(s.top() < min) { //if current top is less than min, this indicates that the top is flag. Which means that we update the min to next closest min and then pop the current top
29
+
min = 2 * min - s.top();
30
+
s.pop();
31
+
}
32
+
else { //else just pop
33
+
s.pop();
34
+
}
35
+
}
36
+
}
37
+
38
+
inttop() {
39
+
if(s.top() < min) { //if top is less than min
40
+
return min;
41
+
}
42
+
elsereturn s.top();
43
+
}
44
+
45
+
intgetMin() {
46
+
return min;
47
+
}
48
+
};
49
+
50
+
/**
51
+
* Your MinStack object will be instantiated and called as such:
0 commit comments