-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1381.cpp
More file actions
43 lines (39 loc) · 918 Bytes
/
1381.cpp
File metadata and controls
43 lines (39 loc) · 918 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
class CustomStack {
public:
stack<int> st;
vector<int> v;
int maxSize;
CustomStack(int max_size) {
v.resize(max_size, 0);
maxSize = max_size;
}
void push(int x) {
if (st.size() < maxSize) {
st.push(x);
}
}
int pop() {
if (st.empty()) return -1;
int index = st.size() - 1;
int result = st.top() + v[index];
st.pop();
if (index > 0) {
v[index - 1] += v[index];
}
v[index] = 0;
return result;
}
void increment(int k, int val) {
int limit = min(k, int(st.size()));
if (limit > 0) {
v[limit - 1] += 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);
*/