Skip to content

Commit

Permalink
Update min-stack.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Jun 10, 2016
1 parent cc58a35 commit 97aaccb
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions C++/min-stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,20 @@ class MinStack2 {
}

void pop() {
if (!elements_.empty()) {
if (!cached_min_with_count_.empty() &&
cached_min_with_count_.top().first == elements_.top()) {
if (--cached_min_with_count_.top().second == 0) {
cached_min_with_count_.pop();
}
if (cached_min_with_count_.top().first == elements_.top()) {
if (--cached_min_with_count_.top().second == 0) {
cached_min_with_count_.pop();
}
auto number = elements_.top();
elements_.pop();
}
elements_.pop();
}

int top() {
return elements_.top();
}

int getMin() {
if (!cached_min_with_count_.empty()) {
return cached_min_with_count_.top().first;
}
return cached_min_with_count_.top().first;
}

private:
Expand Down

0 comments on commit 97aaccb

Please sign in to comment.