Skip to content

Commit 9837102

Browse files
committed
update 155
1 parent 58178d3 commit 9837102

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

cpp/155_Min_Stack/155_Min_Stack.cpp

+11-12
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,32 @@ using namespace std;
88

99
class MinStack {
1010
public:
11-
vector<int> v;
12-
vector<int> min_value;
13-
11+
stack<int> s, s_min;
1412
/** initialize your data structure here. */
1513
MinStack() {
1614

1715
}
1816

1917
void push(int x) {
20-
v.push_back(x);
21-
if (min_value.empty() || x < min_value.back())
22-
min_value.push_back(x);
23-
else
24-
min_value.push_back(min_value.back());
18+
s.push(x);
19+
if (s_min.empty() || s_min.top() >= x) {
20+
s_min.push(x);
21+
}
2522
}
2623

2724
void pop() {
28-
min_value.pop_back();
29-
v.pop_back();
25+
if (s.top() == s_min.top()) {
26+
s_min.pop();
27+
}
28+
s.pop();
3029
}
3130

3231
int top() {
33-
return v.back();
32+
return s.top();
3433
}
3534

3635
int getMin() {
37-
return min_value.back();
36+
return s_min.top();
3837
}
3938
};
4039

0 commit comments

Comments
 (0)