-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1381.cpp
51 lines (46 loc) · 1.01 KB
/
1381.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
#include<bits/stdc++.h>
using namespace std;
class CustomStack {
public:
vector<int> st;
vector<int> increments;
int n;
CustomStack(int maxSize) {
n = maxSize;
}
void push(int x) {
if(st.size() < n){
st.push_back(x);
increments.push_back(0);
}
}
int pop() {
if(st.size() == 0){
return -1;
}
int idx = st.size()-1;
if(idx > 0){
increments[idx-1] += increments[idx];
}
int val = st[idx] + increments[idx];
st.pop_back();
increments.pop_back();
return val;
}
void increment(int k, int val) {
int idx = min(k , (int) st.size())-1;
if(idx >= 0 ){
increments[idx] += 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);
*/
int main(){
return 0;
}