-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily105.cpp
More file actions
63 lines (49 loc) · 1.09 KB
/
Copy pathdaily105.cpp
File metadata and controls
63 lines (49 loc) · 1.09 KB
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
// Solution 1 - unfinished
class CustomStack {
public:
CustomStack(int maxSize) {
}
void push(int x) {
}
int pop() {
}
void increment(int k, int val) {
}
};
/**
* Your CustomStack object will be instantiated and called as such:
* CustomStack* obj = new CustomStack(maxSize);
* obj->push(x);
* int param_2 = obj->pop();
* obj->increment(k,val);
*/
// Solution 2
class CustomStack {
public:
int n;
stack<int> stack;
vector<int> inc;
CustomStack(int n) {
this->n = n;
}
void push(int x) {
if (stack.size() < n) {
stack.push(x);
inc.push_back(0);
}
}
int pop() {
if (stack.empty()) return -1;
if (inc.size() > 1) inc[inc.size() - 2] += inc.back();
int res = stack.top() + inc.back();
stack.pop();
inc.pop_back();
return res;
}
void increment(int k, int val) {
if (!stack.empty()) {
int idx = min(k, (int)inc.size()) - 1;
inc[idx] += val;
}
}
};