-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path155.min-stack.cpp
More file actions
45 lines (35 loc) · 861 Bytes
/
155.min-stack.cpp
File metadata and controls
45 lines (35 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "testharness.h"
#include <string>
#include <string.h>
#include <vector>
#include <deque>
using namespace std;
class MinStack {
public:
void push(int x) {
m_stack.push_back(x);
if (m_minIndex.empty() || m_stack[m_minIndex.back()] > x) {
m_minIndex.push_back(m_stack.size() - 1);
}
}
void pop() {
if (m_stack.empty()) return;
int last = m_stack.size() - 1;
if (last == m_minIndex.back()) {
m_minIndex.resize(m_minIndex.size() - 1);
}
m_stack.resize(last);
}
int top() {
return m_stack.empty() ? 0 : m_stack.back();
}
int getMin() {
return m_minIndex.empty() ? 0 : m_stack[m_minIndex.back()];
}
private:
deque<int> m_stack;
deque<int> m_minIndex;
};
TEST(MinStack, test) {
ASSERT_EQ(2, 1+1);
}