Skip to content

Commit 96d871b

Browse files
committed
fix tests
1 parent 921a4a9 commit 96d871b

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/3_Stack_Queue/min_in_stack.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
* of both the top element and the minimum element after a pop operation.
2929
*/
3030

31+
#include <algorithm>
32+
#include <cassert>
3133
#include <iostream>
3234
#include <optional>
33-
#include <cassert>
3435
#include <stack>
3536
#include <string>
3637

@@ -54,13 +55,18 @@ template <typename T> class StackWithMin {
5455
}
5556

5657
void pop() {
57-
assert(!data_.empty());
58+
if (data_.empty()) {
59+
return;
60+
}
5861
T top_val = data_.top();
5962
if (top_val < *min_value_) {
6063
// Restore the previous minimum.
6164
min_value_ = 2 * *min_value_ - top_val;
6265
}
6366
data_.pop();
67+
if (data_.empty()) {
68+
min_value_.reset();
69+
}
6470
}
6571

6672
std::optional<T> top() const {

0 commit comments

Comments
 (0)