forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
max-stack.cpp
63 lines (53 loc) · 1.46 KB
/
max-stack.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Time: push: O(logn)
// pop: O(logn)
// popMax: O(logn)
// top: O(1)
// peekMax: O(1)
// Space: O(n), n is the number of values in the current stack
class MaxStack {
public:
/** initialize your data structure here. */
MaxStack() {
}
void push(int x) {
const auto idx = idx_to_val_.empty() ? 0 : idx_to_val_.cbegin()->first + 1;
idx_to_val_[idx] = x;
val_to_idxs_[x].emplace_back(idx);
}
int pop() {
const auto val = idx_to_val_.cbegin()->second;
remove(val);
return val;
}
int top() {
return idx_to_val_.cbegin()->second;
}
int peekMax() {
return val_to_idxs_.cbegin()->first;
}
int popMax() {
const auto val = val_to_idxs_.cbegin()->first;
remove(val);
return val;
}
private:
void remove(const int val) {
const auto idx = val_to_idxs_[val].back();
val_to_idxs_[val].pop_back();
if (val_to_idxs_[val].empty()) {
val_to_idxs_.erase(val);
}
idx_to_val_.erase(idx);
}
map<int, int, greater<int>> idx_to_val_;
map<int, vector<int>, greater<int>> val_to_idxs_;
};
/**
* Your MaxStack object will be instantiated and called as such:
* MaxStack obj = new MaxStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.peekMax();
* int param_5 = obj.popMax();
*/